How to check whether an array is empty using PHP?

Here and there a product crash or other surprising conditions can happen in light of a vacant cluster. Thus, it is vital to identify void exhibits in advance and stay away from them. This instructional exercise addresses how to investigate whether or not a specific cluster is unfilled in PHP.

We can likewise utilize the underlying capacity count() to check whether or not a cluster is vacant. The count() work is something similar in its filling in as the sizeof() work. It counts the number of components of an exhibit or a countable article. What we will do is that we will track down the number of components in the exhibit. In the event that the quantity of components in the exhibit is 0 then our cluster is vacant.

An unfilled cluster can at times cause programming crashes or surprising results. To keep away from this, it is smarter to check whether or not a cluster is unfilled ahead of time. There are different strategies and capacities accessible in PHP to check whether or not the characterized or given exhibit is avoid.

How to check whether an array is empty using PHP?

Utilizing void() Function: This capacity decides if a given variable is unfilled. This capacity doesn’t return an admonition assuming a variable doesn’t exist.

Applying the empty() Function

The first method is applying the empty() function as shown in the example below:

<?php
$phone = array();
echo empty($phone) ? "Array is empty.": "Array is not empty.";
?>

The output of this code will indicate that the array is empty.

Applying the count() Function

The next function to use for detecting an empty array is the count() function. This function differs from the one above: it is aimed at counting the elements inside an array.

This function returns 0 once the array is empty. Otherwise, the number of elements will be returned. In the case below, the result is 0. Thus, the given array is empty:

<?php
$phone=array();
echo count($phone);
?>

Now, let’s see another example where the number of the elements is returned:

<?php
$phone=array("00000","11111","22222", "33333");
echo count($phone);
?>

The output of this example is 4. It means that the array includes 4 elements.

Applying the sizeof() Function

The third method is using the sizeof() function. Globally, it is used for checking the array size. When its size is 0, then the array is considered empty. Otherwise, it’s not empty.

Here is an example of using the sizeof() function:

<?php 
   
// Declare an empty array
$empty_array = array();
   
// Use array index to check
// array is empty or not
if( sizeof($empty_array) == 0 )
    echo "Empty Array";
else
    echo "Non-Empty Array";
?>

In the output, it will be indicated that the given array is empty.

Syntax:

bool empty( $var )

Example:

<?php 
 
// Declare an array and initialize it
$non_empty_array = array('URL' => '');
 
// Declare an empty array
$empty_array = array();
 
// Condition to check array is empty or not
if(!empty($non_empty_array))
    echo "Given Array is not empty <br>";
 
if(empty($empty_array))
    echo "Given Array is empty";
?>
Output:

Given Array is not empty 
Given Array is empt

Using count Function: This function counts all the elements in an array. If number of elements in array is zero, then it will display empty array.

Syntax:

int count( $array_or_countable )

Example:

<?php 
  
// Declare an empty array 
$empty_array = array();
  
// Function to count array 
// element and use condition
if(count($empty_array) == 0)
    echo "Array is empty";
else
    echo "Array is non- empty";
?>
Output:

Array is empty

Using sizeof() function: This method check the size of array. If the size of array is zero then array is empty otherwise array is not empty.

Example:

<?php 

  
// Declare an empty array
$empty_array = array();
  
// Use array index to check
// array is empty or not
if( sizeof($empty_array) == 0 )
    echo "Empty Array";
else
    echo "Non-Empty Array";
?>
Output:

Empty Array

Also ReadDifference between Worms and Trojan Horse

Leave a Reply

Your email address will not be published. Required fields are marked *