
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
Different ways of checking if an array is empty or not in PHP
Using the ‘sizeof’ function
Let us see an example −
Example
<?php $empty_arr = array(); if( sizeof($empty_arr) == 0 ) echo "The array is empty!"; else echo "The array is non-empty."; ?>
Output
The array is empty!
An array can be checked to see if it is empty or not in multiple ways. One method is to use the ‘sizeof’ function that sees if the array is empty. If yes, the size would be 0, thereby confirming the array being empty.
Using the ‘empty’ function
Example
<?php $my_arr = array('URL' => 'https://www.medium.com/'); $empty_arr = array(); if(!empty($my_arr)) echo "The array is non-empty <br>"; if(empty($empty_arr)) echo "The array is empty!"; ?>
Output
The array is non-empty The array is empty!
Another method to check if an array is empty or not is to use the ‘empty’ function, that checks to see the contents of the array, and if nothing is present, says it is empty.
- Related Articles
- How to Check if an Array is Empty or Not in Java
- How to check an array is empty or not using jQuery?
- No. of ways to empty an array in JavaScript
- Checking for Null or Empty in Java.
- Check if an array is synchronized or not in C#
- Check if an array is read-only or not in C#
- Different ways to traverse an Array in Java?
- Python Pandas IntervalIndex - Check if an interval that contains points is empty or not
- Python Pandas IntervalIndex - Check if an interval with missing values is empty or not
- MySQL query to check if database is empty or not?
- How to check whether an array is empty using PHP?
- Check if a table is empty or not in MySQL using EXISTS
- Check if an array is descending, ascending or not sorted in JavaScript
- Java Program to check if a string is empty or not
- Python program to check if the string is empty or not

Advertisements