Monday, September 30, 2013

How To Check if an array is empty in PHP?

You can use PHP inbuilt functions; array_filter() and empty() to check if an array is empty.

array_filter() function's default behavior will remove all values from array which are equal to null, 0, '' or false. Otherwise in your particular case empty() construct will always return true if there is at least one element even with "empty" value.

empty() function checks to see whether the variable is empty, null, false, or a representation of zero. It doesn't return true just because the value associated with an array entry is false.

$errors = array_filter($_POST);

if (empty($errors)) {

 echo "array is empty.";
 echo "It is not empty.";

1 comment:

  1. I like this post about array in php. It is very simple to get information about array through few line of codes. Web Development Company Islamabad