The fastest way to check if your PHP array is empty

The fastest way to check if your PHP array is empty

Modified
Nov 2, 2023
Written by
Benjamin Crozat
0
comments
3 minutes
read

The fastest way to check if an array is empty

To check whether a PHP array is empty or not, use the empty() function:

$foo = [];

// true
var_dump(empty($foo));

$bar = ['Foo', 'Bar', 'Baz'];

// false
var_dump(empty($bar));

This is my favorite way of doing it. But there are other methods to check if an array is empty such as:

  1. Using the count() (or sizeof()) function to count the number of elements in the array and check if it’s equal to zero. count() can even count the numbers of entries inside a multidimensional array.
  2. Using the not operator (!). If the array does hold any value, the not operator will return true.

You can stop there, or you can dive deeper and see in detail to use these functions to check for empty arrays.

Other ways to check if an array is empty

The count() function

Another way to check if your array is empty is to use the count() function. The function returns an integer depending on the number of items inside, or zero if it’s empty.

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'],
    ],
];

// 3
$count = count($array, COUNT_RECURSIVE);

// If $count is greater than zero.
if ($count > 0) {
    // The array is not empty.
} else {
    // The array is empty.
}

Learn more about the count() function.

The sizeof() function

sizeof() is an alias of count() and can be used on arrays in the same way. 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

One not super intuitive way to check if your array is not empty is to use the not operator (!).

I had no clue it could check for empty arrays. But here I am, after more than 15 years of PHP, learning yet another basic thing. 😅

$foo = [];

if (! $foo) {
    echo '$foo is empty.';
}
About Benjamin Crozat
Benjamin Crozat

Hi! I’m from the South of France and I’ve been a self-taught web developer since 2006. When I started learning PHP and JavaScript, PHP 4 was still widely used, Internet Explorer 6 ruled the world, and we used DHTML to add falling snow on websites.

Being able to educate myself for free on the web changed my life for the better. Giving back to the community was a natural direction in my career and I truly enjoy it.

Therefore, I decided to take action:

  1. I launched this blog in September 2022 with the goal to be in everyone’s Google search. I get more than tens of thousands of monthly clicks from it and even more visits overall (my analytics dashboard is public by the way).
  2. I also started growing my X (formerly Twitter) account at the same time, which has now over 7,000 followers.
  3. All the content I write is free thanks to my sponsors.

I also want to be completely free with my time and make a living with my own products. In April 2024, I launched Nobinge, a tool to summarize and chat with your content, including YouTube videos.

Believe me, I’m just getting started!

0 comments

You need to be signed in to comment this post.
Sign in with GitHub