PHP

Check Whether an Array is Empty in PHP

In this tutorial, we are going to discuss the method to check whether an array is empty or not in PHP, but first, let’s have a quick introduction to PHP array.

Introduction

The array is used to store multiple data in a linear form permanently, we can access each value of the array by specifying its index value, one more thing about an array is its index starts with a 0.

Syntax

Var_arr =array[Php, 23, Python, 76,  C, 65, C#]

How To Check Whether An Array is Empty or Not in PHP?

Let’s move forward to our main topic, in PHP we have various methods to determine an empty array check out the first method below.

If Condition

We can use the simple if condition to check an empty array see the example below.

Example

<?php
//declaring an empty array
$emp_arr= array();

//using if condition
if (!$emp_arr) {
    echo "No elements in array";
} else {
    echo "it is an array";
}
?>

Code Explanation

In the above code, we declared an empty array, and then we used the if the condition that states if the defined array is not an array with elements then print “No elements in array” else print “it is an array” let’s check the results below.

Output

No elements in array

As you can see in the above output it printed the text for an empty array.

Check out the next method below.

Empty() Function

The empty function is used to check the existence of elements in a specified array, see the example below.

Example

<?php 
// Declaring and initializing an array
$arr = array('Language' => 'PHP', 'Language' => 'PYTHON');
  
// Declare an empty array
$emp_arr = array();
  
// using empty function with if condition
if(!empty($arr))
    echo "defined Array is not empty <br>";
  
if(empty($emp_arr))
    echo "Array is empty";
?>

Code Explanation

In the above code first, we declared an array then we used the empty method of PHP with the if condition, the first condition will check the first declared array if is there any element exists or not, and then it’ll print the text.

In the next condition, it will check whether the emp_arr array is empty or not and then will print the text let’s see the results below.

Output

defined Array is not empty
 Array is empty

As you can see above the empty function returns the exact result for empty and non-empty arrays.

See the next method below.

Count() Function

The Count() function counts the elements in an array, if it does not find any element then it returns any empty array,

Have a look at the example below.

Example

<?php 
   
// Declaring an empty array 
$emp_arr = array();
   
// using count Function 

if(count($emp_arr) == 0)
    echo "no elements in Array";
else
    echo "Array is not Empty";
?>

Code Explanation

In the above code declared an empty array and then we used the count function with an if a condition that will count the elements if they exist and then will print the text otherwise it will print “no elements in array”.

Check out the results below.

Output

no elements in Array

let’s move forward to our next method.

Sizeof() Method

The sizeof function determines the size of an array by counting all elements from an array. See the example below.

Example

<?php 
// Declaring a non-empty array
$arr = array('Language' => 'PHP');
   
// Using sizeof function
if( sizeof($arr) == 0 )
    echo "defined array is Empty";
else
    echo "The Defined Array is Non-Empty";
?>

Code Explanation

In the above code we declared a non-empty array and then we used the sizeof function with the if the condition that will check whether the array is empty or not then will print “defined array is Empty” else it will print “The Defined Array is Non-Empty” check out the results below.

Output

The Defined Array is Non-Empty

Conclusion

In this tutorial we talked about the methods that can be used to check an empty array, firstly we discussed a short introduction to the array, and then we discussed the methods with examples.

We also discussed the code explanations that’s all for today.

I hope you like the tutorial if you have any queries regarding this tutorial so feel free to contact us below. Thanks for reading.

Leave a Comment