How to Check If an Array Is Empty in PHP (2024)

In this article

  • Introduction
  • 1. Using the === [] syntax
  • 2. Using the empty() function
  • 3. Using the count() function
  • 4. Using the sizeof() function
  • 5. Laravel tip: Using the blank() helper function
  • Conclusion

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! 🚀

How to Check If an Array Is Empty in PHP (2024)

FAQs

How to Check If an Array Is Empty in PHP? ›

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

How to evaluate an array is empty in PHP? ›

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.

How do you check if an array in object is empty? ›

How to Check If an Array Is Empty in JavaScript
  1. Using the length property with the === operator.
  2. Using the length property with the ! operator.
  3. Using the Array.isArray() method.
  4. Gotcha: Using the === [] syntax.
Jan 8, 2024

How to check if array has some value in PHP? ›

Using array_key_exists() function

The array_key_exists() function checks if a specific key exists in an array. It takes two parameters: the key to check and the array to search in. If the key exists, it returns true; otherwise, it returns false. This approach is suitable for associative arrays.

How to check array values empty in PHP? ›

The fastest way to check if an array is empty

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. Using the not operator ( ! ).

How to check if a value is empty in PHP? ›

PHP empty() Function

The empty() function checks whether a variable is empty or not. This function returns false if the variable exists and is not empty, otherwise it returns true.

How do you check if a cell array is empty? ›

To test if any element in the cell array is empty, you have to loop through the array. You can do that in one line with: any(cellfun(@(x) isempty(x),B))

How do you check if an array has any value? ›

Description. The includes() method returns true if an array contains a specified value. The includes() method returns false if the value is not found. The includes() method is case sensitive.

How to check if an ArrayList is empty? ›

The isEmpty() method of ArrayList in java is used to check if a list is empty or not. It returns true if the list contains no elements otherwise it returns false if the list contains any element.

How do you check if a character array is empty? ›

  1. In C and C++, you can check if a char array is empty by using the strlen function, which returns the length of a null-terminated string. ...
  2. #include <cstring>
  3. char str[10] = {'\0'}; if (strlen(str) == 0) { // str is empty }
  4. Alternatively, you can also use the following code to check if a char array is empty:
Jun 20, 2020

How do you check if an array contains empty string? ›

You can test a string array for empty strings using the == operator. You can create an empty string using double quotes with nothing between them ( "" ). Note that the size of str is 1-by-1, not 0-by-0. However, str contains zero characters.

How do you check if an array contains an object? ›

How to check if an array includes an object in JavaScript ?
  1. Using includes() Method.
  2. Using some() Method.
  3. Using filter() Method.
  4. Using findIndex() Method.
  5. Using Lodash _.find() Method.
  6. Using the spread operator (…) and Math.floor():
  7. Using Array.prototype.find Method.
Jun 10, 2024

How to check array object value exists in PHP? ›

PHP array_key_exists() Function

The array_key_exists() function checks an array for a specified key, and returns true if the key exists and false if the key does not exist.

How to check if array values are equal in PHP? ›

When the order of the array elements is not important, two methods can be applied to check the array equality which is listed below:
  1. Use the sort() function to sort an array element and then use the equality operator.
  2. Use array operator ( == ) in case of Associative array.
Dec 3, 2021

How to find value in array in PHP? ›

The array_search() is an inbuilt function in PHP that is used to search for a particular value in an array, and if the value is found then it returns its corresponding key. If there are more than one values then the key of the first matching value will be returned.

How to define an empty array in PHP? ›

Syntax to create an empty array:

In other words, the initialization of new array is faster, use syntax var first = [] rather while using syntax var first = new Array(). The fact is being a constructor function the function Array() and the, [] is a part of the literal grammar of array.

How to find missing value in array in PHP? ›

A function named 'absent' is defined that checks to see the minimum number and the maximum number and generates an array within that range. The function then returns the difference between this array and the original array, using the 'array_diff' function, that gives the missing elements from the array.

How to check if a variable is an array in PHP? ›

The is_array() function checks whether a variable is an array or not. This function returns true (1) if the variable is an array, otherwise it returns false/nothing.

How to find value in array and remove it in PHP? ›

If you want to delete an element from an array but you only know its value, you can use array_search() to find the key of the element, and then use unset() to remove the key-value pair. Note that if there are duplicate elements in the array, array_search() will only return the first match.

Top Articles
Latest Posts
Article information

Author: Jamar Nader

Last Updated:

Views: 5669

Rating: 4.4 / 5 (75 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Jamar Nader

Birthday: 1995-02-28

Address: Apt. 536 6162 Reichel Greens, Port Zackaryside, CT 22682-9804

Phone: +9958384818317

Job: IT Representative

Hobby: Scrapbooking, Hiking, Hunting, Kite flying, Blacksmithing, Video gaming, Foraging

Introduction: My name is Jamar Nader, I am a fine, shiny, colorful, bright, nice, perfect, curious person who loves writing and wants to share my knowledge and understanding with you.