Tip & Tricks on Checking Whether php Check if Array Empty

php check if array empty

 It is often necessary to check if a given array is empty before performing any actions on it. In this blog post, we will describe a few different ways to tackle this problem using the PHP programming language.

Method 1: Using the count() function

The most straightforward  php check if array empty way to check if an array is empty is by using the count() function. This function returns the number of elements in an array. So, to check if an array is empty, we can simply compare the return value of the count() function to 0.

Here is an example:

$arr = array(1, 2, 3);

if(count($arr) > 0){

//array is not empty

} else {

//array is empty


Method 2: Using the empty() function

Another way to check if an array is empty is by using the empty() function. This function takes a variable as a parameter and returns TRUE if the variable is empty and FALSE if it is not.

Keep in mind that a variable can be considered “empty” for several reasons: it has never been assigned a value, it has been assigned a NULL value, it has been assigned an empty string, etc. So, when using the empty() function on an array, we need to make sure that we are only considering arrays that have never been assigned any values as “empty.”

Here’s a sample:

first code is : $arr = array(); //has never been assigned any values

if(!empty($arr)){ //will return FALSE because the array IS indeed empty

//this code will never be executed because $arr IS empty

} else { //this code WILL be executed because $arr IS empty

// array is empty


Method 3: Using sizeof() or sizeof($var) / sizeof(current($var))

A third way to check whether an array is size=0 (which would mean it’s “empty”), you could use either sizeof($var) or sizeof($var)/sizeof(current($var)). If the current($var) means “the first element of the array”, then both of these expressions would give us what we’re looking for – size=0. It might look like this in your code.

$arr = array(); //has never been assigned any values

if(sizeof($arr) == 0 || sizeof($arr)/sizeof(current($arr)) == 0){

//array is empty

} else {

//  this code will never be executed again because $arr IS empty


There is use also can you, Finally. Not is it if FALSE and empty is variable the if TRUE returns and parameter an as variable a take which, function() empty the use to is way Another. Array an in elements of number the returns which, function() count the use to is way straightforward most The. PHP using empty is an array and if check to ways different few a Describe will we post, blog this is in.


As you might have noticed from reading this blog post, there are several ways to go about checking whether or not an array contains any values at all using PHP. All three methods outlined above are perfectly valid approaches; which one you choose will likely come down to personal preference or readability, or the maintainability concerns for your particular project. We are grateful to you for taking the time to read our blog.



Please enter your comment!
Please enter your name here