How to Check Whether An Array is Empty Using PHP?

Last Updated : 07 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In PHP, arrays are commonly used data structures that can hold multiple values. However, sometimes it is necessary to check whether an array is empty, i.e., whether it has no elements. This is useful when the array is filled with data automatically, and you need to check if it has any data before performing any actions or checks.

Methods to Check Whether An Array is Empty

Below are the methods to check whether an array is empty or not using PHP:

1. Using the ===[] Syntax

One of the simplest ways to check if an array is empty is by directly comparing it to an empty array using the === operator. This approach is very clear and concise, and it ensures that the variable is specifically an empty array (not an empty string or null).

Now let's understand with the help of example:

PHP
<?php
$array1 = [];
$array2 = [1, 2, 3];
var_dump($array1 === []); 
var_dump($array2 === []); 
?>

Output
bool(true)
bool(false)

2. Using the empty() Function

The empty() function is a built-in PHP function that checks if a variable is empty. This includes arrays, and it returns true if the array has no elements or is not set.

Now let's understand with the help of example:

PHP
<?php 
$empty_array = array(); 

if(!empty($non_empty_array)) 
    echo "Given Array is not empty <br>"; 
if(empty($empty_array)) 
    echo "Given Array is empty"; 
?>

Output
Given Array is empty

3. Using the count() Function

Another common approach is to use the count() function, which returns the number of elements in the array. If the count is 0, the array is empty.

Now let's understand with the help of example:

PHP
<?php
$array = array(); // Empty array

if (count($array) == 0) {
    echo "The array is empty.";
} else {
    echo "The array is not empty.";
}
?>

Output
The array is empty.

4. Using the sizeof() function

The sizeof() function in PHP is similar to count() function. It works the same way as count() and can be used to check if an array is empty by checking if the number of elements is zero.

Now let's understand with the help of example:

PHP
<?php 
$empty_array = array(); 

if( sizeof($empty_array) == 0 ) 
    echo "Empty Array"; 
else
    echo "Non-Empty Array"; 
?>

Output
Empty Array

5. Using the array_filter()

array_filter() removes all "empty" values from an array (i.e., values considered as false in PHP such as 0, null, false, etc.). When no callback function is provided, it filters out those values. You can check whether the filtered array is empty.

Now let's understand with the help of example:

PHP
<?php
  $names = [];

if (count(array_filter($names)) === 0) {
    echo "The array is empty.";
} else {
    echo "The array is not empty.";
}

?>

Output
The array is empty.

Best Practises

  • Use empty() for a simple and efficient check.
  • Use count() if you need to know the array’s size.
  • Avoid direct comparison to [], unless necessary.
  • Use array_filter() if dealing with arrays containing empty or null values.

Conclusion

In conclusion, there are multiple ways to check if an array is empty in PHP, each serving a unique purpose depending on the context of your project. The === [] syntax provides a strict check for an empty array, while the empty() function is useful for detecting empty values and other falsy data types. For cases where you need to count elements, both the count() and sizeof() functions are reliable options. If you want to clean the array before checking its size, array_filter() can be used to remove falsy values.


Similar Reads