Check for an empty array in PHP
This post will discuss how to determine whether an array is empty in PHP.
1. Using empty() function
A variable is considered to be empty in PHP if it doesn’t exist, or if its value equals false. The empty() function returns true if a variable is empty, false otherwise. You can use the empty() function to check for empty arrays as well. It returns true if there are no elements present in the array and false otherwise.
|
1 2 3 4 5 6 7 |
<?php $array = array(); if (empty($array)) { echo "Array is empty"; } ?> |
The empty() function will return true for all falsey values (like false, 0, “”, null, array(), etc.). If you specifically want to check for empty arrays, you can first check for an array using is_array() function.
|
1 2 3 4 5 6 7 |
<?php $array = array(); if (is_array($array) && empty($array)) { echo "Array is empty"; } ?> |
2. Using negation operator
Because an empty array is false in PHP, the call to empty() can be replaced with the logical negation operator (!). The logical negation operator implicitly converts its operand to type bool, and returns a boolean value that is opposite to that of the converted operand. As with the empty() function, you can determine whether the given variable is an array with is_array() function.
|
1 2 3 4 5 6 7 |
<?php $array = array(); if (is_array($array) && !$array) { echo "Array is empty"; } ?> |
3. Using identity comparison operator
Alternatively, you can use the identity comparison operators (=== and !==) to check for empty arrays in PHP. The expression $a === $b returns true if $a is equal to $b, and they are of the same type. You can compare your variable against the array() construct with === operator, which returns true only for empty arrays and false otherwise.
|
1 2 3 4 5 6 7 |
<?php $array = array(); if ($array === array()) { echo "Array is empty"; } ?> |
Since PHP 5.4, you can use the short array syntax []. It works in a similar way as the array() construct, but avoids the overhead of calling the function.
|
1 2 3 4 5 6 7 |
<?php $array = array(); if ($array === []) { echo "Array is empty"; } ?> |
4. Using count() function
You can also count the total number of elements in the array. The count() or sizeof() function will work for arrays or objects that implement Countable. For other falsey values, count() throws Fatal error: Uncaught TypeError: count(): The first argument ($value) must be of the type Countable|array.
|
1 2 3 4 5 6 7 |
<?php $array = array(); if (count($array) === 0) { echo "Array is empty"; } ?> |
5. Using array_filter() function
If you need to check if the array contains all falsey values, you can pass your array to the array_filter() function and compare the resultant array against array() construct or [] syntax with the === operator. The array_filter() function filters elements of an array using a callback function. If no callback is supplied, all falsey values will be removed from the array. The following solution demonstrates this:
|
1 2 3 4 5 6 7 8 9 10 11 |
<?php $array = array(false, '', 0); if (array_filter($array) === []) { echo "Array contains all falsey values"; } /* Output: Array contains all falsey values */ ?> |
That’s all about checking for an empty array in PHP.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)