Get your next remote job on LaraJobs.
1 contributor Edit on GitHub PHP

The fastest way to check if your PHP array is empty

Table of contents:

The fastest way to check if your PHP array is empty

The fastest way to check if an array is empty

To ensure that a PHP array is empty, 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 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.';
}
Benjamin Crozat

Written by Benjamin Crozat

Indie hacker, blogger, and AI enthusiast building things with the TALL stack. 🔥

Follow me on:

Recommended articles

Print an array with PHP (+ Laravel) Print an array with PHP (+ Laravel)

Debugging requires dissecting everything. Here's a list of all the one-line of code built-in ways to print arrays in PHP (and even Laravel-specific helpers).

Modified on Jun 24, 2023

14 Laravel Collections tips to refactor your codebase 14 Laravel Collections tips to refactor your codebase

Laravel Collections make arrays more powerful and convenient to work with. This article provides tons of quick tips to instantly make your codebase better.

Modified on Aug 27, 2023

PHP 8.3 is out, now! Here's what's new and changed. PHP 8.3 is out, now! Here's what's new and changed.

PHP 8.3 was released on November 23, 2023, and as usual, you need to be up to date with new features and breaking changes for easier transitions.

Modified on Nov 23, 2023

PHP
Easily convert a PHP array to JSON Easily convert a PHP array to JSON

Convert PHP arrays to JSON with `json_encode()`. Ideal for data exchange, storing data, and API communication.

Published on Sep 16, 2023

PHP

Gold sponsors New

  • Wire Elements
    Beautiful handcrafted Livewire components.
    Check site
Your business here

Partners

If you buy from one of my partners below, I will be compensated at no cost to you. These are services I use or used, and 100% stand behind.

  • Scalable and reliable VPS hosting.
    Bonus: $200 of free credits
    Check site
  • The Google Analytics alternative without compromise.
    Free trial: 30 days
    Bonus: $10 off your first invoice
    Check site
  • Flare
    Track PHP and JavaScript errors in one place.
    Free trial: 10 days
    Check site
  • Keep track of your Google rankings.
    Free trial: 7 days
    Promo code: WELCOME30
    Check site
- / -