Benjamin Crozat The web developer life

Is an array empty in PHP? A few ways to make sure of it.

Benjamin Crozat Benjamin Crozat2 minutes read

Before you start reading this article, did you know 76 persons subscribed to my newsletter?

Join them and enjoy free content about the art of crafting websites!

Is an array empty in PHP? A few ways to make sure of it.
Table of contents

To ensure that a PHP array is empty, you can use any of the following methods:

  1. Use the empty() function to check if the array is empty and return a boolean value.
  2. Use the count() (or sizeof()) function to count the number of elements in the array and check if it is equal to zero.
  3. Use the not operator (!). If the array does hold any value, the not operator will return true.

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 = [];
// true
if (empty($foo)) {
$bar = ['Foo', 'Bar', 'Baz'];
// false
if (empty($bar)) {

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.

// 3
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'],
// 1
$count = count($array);
// 3
$count = count($array, COUNT_RECURSIVE);
// If $count is greater than zero, then your array is not empty.
if ($count > 0) {

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:

// 3
echo sizeof(['Foo', 'Bar', 'Baz']);

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.';

Other posts to read

Debugging requires dissecting everything. Here's a list of all the one-line of code built-in ways to print arrays in PHP.

Let's review some quick wins that Laravel Collections provide to instantly make your codebase better.

Redirects in PHP are simple. I will guide you step by step and show you how to dodge some traps. Finally, we'll learn the nuance between 301 and 302 redirects.

When in doubt, clear the cache. In this article, you'll learn about how to clear every cache Laravel uses.

Learn why the "Using $this when not in object context" error happens, and let me show you the only way to fix.

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

Learning a framework can be overwhelming. Time and practice will make you a master. Here are some best practices to help you toward your goal.

Learn why the "Invalid argument supplied for foreach()" warning happens, and let me show you multiple ways to fix it.

Laravel Dropbox Driver is a new storage you can add to your Laravel application and use it for all sorts of things.

Choose your web hosting provider and deploy your next Laravel project quickly and without any DevOps cost.