Loading...
How to check array is empty in PHP

4 Easy Ways to Check If an Array Is Empty in PHP

Frequently, in our coding journey, we face many common scenarios. One of them is to verify whether the PHP array is empty or not in any kind of array created by you or received from an API.

4 Easy Ways to Check if an Array is Empty
  1. The === [ ] syntax
  2. The empty() function
  3. The count() function
  4. The sizeof() function

In this blog post, we'll explain the four most common ways to check if a PHP array is empty.

1. The === [ ] syntax

Use the === [ ] syntax to check if your PHP array is empty. It is a simple and short technique to find an empty array in PHP. This system is easy to use and keep in mind because of its simple syntax and clear logic. The reason we are using === instead of == is to confirm that the variable we are looking at is actually an empty array and not just an empty string or integer. If the array is empty, the expression will return true.
$names = [ ];

$isEmpty = $names === [ ]; // true

//And if the array is not empty, the expression will return fall.

$names = [‘Ram’,’Rohan’ ];

$isEmpty = $names === [ ]; // false

2. The empty() Function

The second method for checking the PHP array is empty or not, is to use an empty function. This in-built PHP function returns a boolean value that indicates whether the variable passed to it is null or empty. Remember that the empty() PHP function does not work to check if any laravel collection is empty. If the array is empty, the expression will return true.
$names = [ ];

$isEmpty = empty($names); // true

//And if the array is not empty, the expression will return fall.

$names = ['Ram', 'Rohan'];

$isEmpty = empty($names); // false

3. The count() Function

The third way to check whether your PHP array is empty or not is to apply the count function. This function does not reply to values like true or false. If your array is empty, it replies value 0 (Zero) In the example, you can see that the PHP array is empty and returns a value of 0.
$names = [ ];
 
$isEmpty = count($names) === 0; // true

//If the array is not empty, the function must return an integer that is greater than 0

$names = ['Ram', 'Rohan'];

$isEmpty = count($names) === 0; // false

4. The sizeof() Function

The fourth function is the sizeof() function for finding empty PHP arrays in your database. We recommend that you not use the sizeof function because some developers found that it gives the impression that it returns the array size in bytes. If you want to use the sizeof the function, you can do so to find whether or not the array is empty. If this array is empty, the function returns 0.
$names = [];
 
$isEmpty = sizeof($names) === 0; // true

//If the array is not empty, the function must return an integer that is greater than 0.

$names = ['Ram', 'Rohan'];

$isEmpty = sizeof($names) === 0; // false

Essence

We hope that, out of these four options, anyone can help you solve your queries. Now, you can find your empty PHP array in different ways. We wrote this guide for developers who need help with the common issue of finding an empty PHP array within their code. Elightwalk Technology provides multiple solutions for developers and businesses to learn, update, and develop new websites with top solution formulas. Contact us for any technological queries about developing related solutions and guidance. Our development team supports you and leads you to the solution to your projects.

Jayram Prajapati
Full Stack Developer

Jayram Prajapati brings expertise and innovation to every project he takes on. His collaborative communication style, coupled with a receptiveness to new ideas, consistently leads to successful project outcomes.

Most Visited Blog

Blog
Magento vs. Shopify: A Comprehensive Comparison

For e-commerce merchants and business owners, the choice between Magento and Shopify is a very important decision in developing a successful online store. In this in-depth comparison, we explore the unique features and advantages of each platform to help you make an informed decision based on your business goals and needs.

Read More
Blog
Constructor Property Promotion in PHP 8
Explore the power of Constructor Property Promotion in PHP 8 to streamline class property declaration and initialization. Learn the advantages and see an example demonstrating how to effortlessly add custom values during object creation for efficient coding practices.
Read More
Blog
How to delete all products and categories in Magento
Manage your Magento database with a SQL script to delete all categories and products. Prioritise backups, test in a controlled environment and follow best practices for responsible and secure usage.
Read More