Is your PHP array empty? A few ways to make sure of it.
Hundreds of developers subscribed to my newsletter.
Join them and enjoy free content about the art of crafting websites!
To ensure that a PHP array is empty, you can use any of the following methods:
- Use the
empty()
function to check if the array is empty and return a boolean value. - Use the
count()
(orsizeof()
) function to count the number of elements in the array and check if it is equal to zero. - Use the not operator (
!
). If the array does hold any value, the not operator will returntrue
.
Table of contents

The empty()
function
The empty()
function determines if a value is empty or not. It simply returns true
if it’s empty, or false
if it’s not.
This is my favorite way to check if an array is empty in PHP.
$foo = []; // trueif (empty($foo)) { //} $bar = ['Foo', 'Bar', 'Baz']; // falseif (empty($bar)) { //}
Learn more about the empty()
function.
The count()
function
The count()
function counts the number of entries in an array and returns it as an integer.
You can even use it with Countable objects.
echo count(['Foo', 'Bar', 'Baz']);
For multidimensional arrays, there’s a second parameter for which you can use the COUNT_RECURSIVE
constant to recursively count the numbers of items.
$array = [ 'Foo' => [ 'Bar' => ['Baz'], ],]; $count = count($array, COUNT_RECURSIVE); // If $count is greater than zero, then your array is not empty.if ($count > 0) { //}
Learn more about the count()
function.
The sizeof()
function
sizeof()
is an alias of count(). PHP actually has a lot of aliases for various functions.
There’s nothing to add, you already know how to use it:
echo sizeof(['Foo', 'Bar', 'Baz']);
Learn more about the sizeof()
function.
The not (!
) operator
This one is simple. You are probably used to the not (!
) operator. I didn’t know it could check for empty arrays, but here I am, after 15 years of PHP learning yet another basic thing.
$foo = []; if (! $foo) { echo '$foo is empty.';}