Home PHP PHP: Check If Array is Empty or Null
Author:

An empty array can cause your software to crash or stop the website to functions correctly. You can check an array value to see if it is empty or null before accessing it so that your site won't have an error.

Here's how:

1. Before access the array value, use isset or empty function.

Sample code:

Empty Function

if(empty($lising)){
echo "This array has a value: " . $listing['features'];
}else{
echo "Unfortunately this array is empty.";
}

Isset function

if(isset($listing)){
echo "This array has a value: " . $listing['features'];
}else{
echo "Unfortunately this array is empty.";
}

You could also use the count function see if there has been any item added to the array. See example below.

 

if( count($yourarray) && empty($listing['feature']) )
{
//Perform your code
}

To check just for a single element:

if(!empty($listing['feature']))
{

//This element is not empty.

}

Did you like this tip?



Comments (0)
Write comment
Your Contact Details:
Comment:
Security
Please input the anti-spam code that you can read in the image.

"