Introduction
As a PHP developer, a common task I come across regularly is to check whether a PHP array is empty or not. This could be an array that I've created myself or one that I've received from an API.
In this Quickfire post we're going to take look at some of the most common ways to check if a PHP array is empty.
I've also published two other articles that cover how check if a Laravel Collection is empty or check if a JavaScript array is empty. Both articles cover common gotchas that catch me out often and have caused bugs for me in the past!
1. Using the === []
syntax
My preferred way of checking if a PHP array is empty is to use the === []
syntax. The reason I like doing this is because it's short, concise, and easy to read. Since we're using ===
instead of ==
, we're checking that the variable we're checking is in fact an empty array and not just an empty string or integer.
If the array is empty, the expression will return true
like so:
1$names = [];2 3$isEmpty = $names === []; // true
If the array is not empty, the expression will return false
like so:
1$names = ['Ash', 'Allen'];2 3$isEmpty = $names === []; // false
2. Using the empty()
function
Another way to check if a PHP array is empty to use the empty
function. This function is a built-in PHP function that returns a boolean value indicating whether the variable passed to it is empty or not.
If the array is empty, the function will return true
like so:
1$names = [];2 3$isEmpty = empty($names); // true
If the array is not empty, the function will return false
like so:
1$names = ['Ash', 'Allen'];2 3$isEmpty = empty($names); // false
As a side note for any of my Laravel readers, it's important to remember that you shouldn't use the empty()
PHP function for checking if a Laravel collection is empty. It's a common error I come across that can cause bugs in your code. I've covered this in more detail in another article (How to Check If a Laravel Collection Is Empty) if you're interested in finding out more.
3. Using the count()
function
Another way of checking if a PHP array is empty is to use the count
function. This function returns the number of items in the array as an integer which we can then check against.
If the array is empty, the function will return 0, so we can check against this like so:
1$names = [];2 3$isEmpty = count($names) === 0; // true
If the array is not empty, the function will return an integer greater than 0, so we can check against this like so:
1$names = ['Ash', 'Allen'];2 3$isEmpty = count($names) === 0; // false
4. Using the sizeof()
function
As I covered in another article, the sizeof
function is an alias of the count
function, meaning that it does the exact same thing. It returns the number of items in the PHP array as an integer which we can then check against.
It's likely if you try and use the sizeof
function in your code, your IDE will suggest that you use the count
function instead. I also prefer to avoid using the sizeof
function because some developers feel it gives the impression that it returns the size of the array in bytes.
However, if you do want to use the sizeof
function, you can still use it to check whether the array is empty or not.
If the array is empty, the function will return 0, so we can check against this like so:
1$names = [];2 3$isEmpty = sizeof($names) === 0; // true
If the array is not empty, the function will return an integer greater than 0, so we can check against this like so:
1$names = ['Ash', 'Allen'];2 3$isEmpty = sizeof($names) === 0; // false
5. Laravel tip: Using the blank()
helper function
For any of my reader that are using Laravel, you can also use the blank
helper function to check whether an array is empty is PHP. This is a helper function that is included by the framework and is really helpful for checking whether different types of variables are empty or not.
Let's take a look at how we can use it to check whether a PHP array is empty, and then we'll look at how it works under the hood.
If the array is empty, the function will return true
, so we can check against this like so:
1$names = [];2 3$isEmpty = blank($names); // true
If the array is not empty, the function will return false
, so we can check against this like so:
1$names = ['Ash', 'Allen'];2 3$isEmpty = blank($names); // false
Now let's check out how the blank
function works under the hood. As of Laravel 10, the function looks like so:
1function blank($value) 2{ 3 if (is_null($value)) { 4 return true; 5 } 6 7 if (is_string($value)) { 8 return trim($value) === ''; 9 }10 11 if (is_numeric($value) || is_bool($value)) {12 return false;13 }14 15 if ($value instanceof Countable) {16 return count($value) === 0;17 }18 19 return empty($value);20}
As we can see, the function performs numerous checks against the passed in $value
parameter. In our particular case (checking if an array is empty), the function will use the final line in the function to check whether the array is empty or not:
1function blank($value)2{3 // ...4 5 return empty($value);6}
As we can see, this is the same as using the empty
function that we covered earlier in this article.
Conclusion
Hopefully, this article has given you a quick insight into different ways that you can check whether a PHP array is empty. It should have also given you a quick insight into how the blank
helper function works in Laravel.
If you enjoyed reading this post, I'd love to hear about it. Likewise, if you have any feedback to improve the future ones, I'd also love to hear that too.
You might also be interested in checking out my 220+ page ebook "Battle Ready Laravel" which covers similar topics in more depth.
Or, you might want to check out my other 440+ ebook "Consuming APIs in Laravel" which teaches you how to use Laravel to consume APIs from other services.
If you're interested in getting updated each time I publish a new post, feel free to sign up for my newsletter below.
Keep on building awesome stuff! 🚀