How to Check Whether An Array is Empty Using PHP?
Last Updated :
25 Jun, 2025
Arrays are commonly used data structures that can hold multiple values. Sometimes it is necessary to check if an array is empty or not, and it is an important task to avoid errors when working with data. PHP offers several easy and effective ways to check if an array is empty. Below are the most common methods.
Methods to Check An Empty Array
1. Using the ===[] Syntax (Most Used)
One of the simplest ways to check if an array is empty is by directly comparing it to an empty array using the strict equality operator (===
). This checks both the type and the value, making it an easy way to determine if the array is truly empty.
Example:
PHP
<?php
$arr1 = [];
$arr2 = [1, 2, 3];
// Check if arr1 is empty
var_dump(empty($arr1));
// Check if arr2 is empty
var_dump(empty($arr2));
?>
Outputbool(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.
Example:
PHP
<?php
$arr = array();
// Check if the $arr is not empty
if (!empty($arr)) {
echo "Given Array is not empty <br>";
}
// Check if the $arr is empty
if (empty($arr)) {
echo "Given Array is empty";
}
?>
OutputGiven 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 and
the array is empty.
Example:
PHP
<?php
$arr = array();
// Check if the array has zero elements using count()
if (count($arr) == 0) {
echo "The array is empty.";
} else {
echo "The array is not empty.";
}
?>
OutputThe 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.
Example:
PHP
<?php
$arr = array();
// Check if the array size is 0 using sizeof()
if (sizeof($arr) == 0) {
echo "Empty Array";
} else {
echo "Non-Empty Array";
}
?>
5. Using the array_filter()
The array_filter()
function is used to filter out empty values from an array. If the result is an empty array, it means the original array was empty or had only empty values.
Example:
PHP
<?php
$arr = [];
// Use array_filter() to remove any empty values and then count the array
if (count(array_filter($arr)) === 0) {
echo "The array is empty.";
} else {
echo "The array is not empty.";
}
?>
OutputThe array is empty.
Best Practices
- Use
empty()
for a simple and efficient check if an array is empty. - Use
count()
if you need to know the array's size. - Use
array_filter()
when dealing with arrays containing empty, null or false values to filter out unwanted element
Conclusion
There are multiple ways to check if an array is empty or not 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 the elements, both the count()
and sizeof()
functions are useful. If you want to clean the array before checking the size, you can use the array_filter()
function to remove falsy values.