Maximize your Laravel blog's potential with SEO best practices and reach 10K monthly clicks on Google.
Preview the course for free
Benjamin Crozat New!
Benjamin Crozat The art of crafting web applications

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

Benjamin Crozat — Updated on
Artboard

Hundreds of developers subscribed to my newsletter.
Join them and enjoy free content about the art of crafting websites!

Powered by

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.

Table of contents

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

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)) {
//
}

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

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).

switch, case, and break. What are all these? When should you use it instead of if? What are its pros and cons?

Learning a framework can be overwhelming, but time and execution will make you a master. Here are some best practices to help you toward your goal.

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

Take your code to the next level, thanks to exceptions. Handle errors in a more graceful way within try and catch blocks.

Store and manage files on Dropbox and use it to back up your Laravel app automatically. Compatible with PHP 8.1+ and Laravel 9+.

Laravel 10 has been released on February 14, 2023. Let's dive into every relevant new feature and change.

Start leveraging the power of AI today. It enables developers to do incredible things, and many startups build products around it.

Nailing a Laravel job interview can be a daunting task, but with the right preparation and mindset, you can set yourself up for success.

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.

Powered by