249

I have the following code

<?php

$error = array();
$error['something'] = false;
$error['somethingelse'] = false;

if (!empty($error))
{
    echo 'Error';
}
else
{
    echo 'No errors';
}

?>

However, empty($error) still returns true, even though nothing is set.

What's not right?

5
  • 6
    Works correctly for me in PHP 5.3 on Windows - I get Error
    – Pekka
    Commented Nov 30, 2011 at 16:05
  • stackoverflow.com/questions/4139301/… check answer no 2
    – TheTechGuy
    Commented Nov 30, 2011 at 16:06
  • Is the output of this code "Error" or "No errors"? Commented Nov 30, 2011 at 16:09
  • 1
    What are you trying to achieve? When I run this code, I get Error which is correct because the array has keys and hence is not empty. Do you want false to somehow equate to "not there"? Commented Nov 30, 2011 at 16:11
  • 1
    I get Error too. I wanted it so that if $error['something'] = true, then only will $error return true.
    – gbhall
    Commented Nov 30, 2011 at 16:16

12 Answers 12

427

There are two elements in array and this definitely doesn't mean that array is empty. As a quick workaround you can do following:

$errors = array_filter($errors);

if (!empty($errors)) {
}

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.

4
  • 2
    array_filter($input) also removes empty arrays from $input.
    – jan
    Commented Aug 19, 2013 at 13:38
  • 60
    This could be trimmed down to if (array_filter($errors)) {} Commented Nov 20, 2013 at 8:19
  • 2
    array_keys($errors, true) would do the same as this filter, I think. (Most people don't use the second argument, but you can search with it.) Commented Dec 1, 2013 at 4:22
  • or array_sum(array_values($errors)) would just tell you how many errors you have. Commented Dec 1, 2013 at 4:23
72

You can also check it by doing.

if(count($array) > 0)
{
    echo 'Error';
}
else
{
    echo 'No Error';
}
2
  • 5
    You don't need to count the elements or use empty(). A one-liner like php -r 'echo array() ? "T":"F";' will show the truthiness for an empty or non-empty array.
    – jerseyboy
    Commented May 16, 2014 at 13:08
  • empty() handles FALSE and isset(), so you may want empty() for that reason. Commented Aug 21, 2015 at 19:26
16

Try to check it's size with sizeof if 0 no elements.

3
  • 18
    sizeof is an alias of count.
    – pmagunia
    Commented Jun 28, 2014 at 22:11
  • 10
    Reminder: sizeof is on the deprecation list, see wiki.php.net/rfc/deprecations_php_7_1 Commented Jul 28, 2016 at 10:58
  • In this case, count (or sizeof) would return two, due to the two values which are set to false. Commented Dec 3, 2018 at 13:38
16

PHP's built-in 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, in this case the array has actual elements in it and that's all that's evaluated.

If you'd like to check whether a particular error condition is set to true in an associative array, you can use the array_keys() function to filter the keys that have their value set to true.

$set_errors = array_keys( $errors, true );

You can then use the empty() function to check whether this array is empty, simultaneously telling you whether there are errors and also which errors have occurred.

8

array with zero elements converts to false

http://php.net/manual/en/language.types.boolean.php

0
5

However, empty($error) still returns true, even though nothing is set.

That's not how empty() works. According to the manual, it will return true on an empty array only. Anything else wouldn't make sense.

3

From the PHP-documentation:

Returns FALSE if var has a non-empty and non-zero value.

The following things are considered to be empty:

"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
var $var; (a variable declared, but without a value in a class)
1
2

function empty() does not work for testing empty arrays! example:

$a=array("","");
if(empty($a)) echo "empty";
else echo "not empty"; //this case is true

a function is necessary:

function is_array_empty($a){
foreach($a as $elm)
if(!empty($elm)) return false;
return true;
}

ok, this is a very old question :) , but i found this thread searching for a solution and i didnt find a good one.

bye (sorry for my english)

1
  • This is a best way of array empty checking. Or you can use $set_errors = array_keys( $errors, true ); Commented May 27, 2015 at 5:25
2

hi array is one object so it null type or blank

   <?php
        if($error!=null)
            echo "array is blank or null or not array";
    //OR
       if(!empty($error))
           echo "array is blank or null or not array";
    //OR
     if(is_array($error))
           echo "array is blank or null or not array";
   ?>
1

I can't replicate that (php 5.3.6):

php > $error = array();
php > $error['something'] = false;
php > $error['somethingelse'] = false;
php > var_dump(empty($error));
bool(false)

php > $error = array();
php > var_dump(empty($error));
bool(true)
php >

exactly where are you doing the empty() call that returns true?

1
<?php
if(empty($myarray))
echo"true";
else
echo "false";
?>
1

In PHP, even if the individual items within an array or properties of an object are empty, the array or object will not evaluate to empty using the empty($subject) function. In other words, cobbling together a bunch of data that individually tests as "empty" creates a composite that is non-empty. Use the following PHP function to determine if the items in an array or properties of an object are empty:

function functionallyEmpty($o)
{
  if (empty($o)) return true;
  else if (is_numeric($o)) return false;
  else if (is_string($o)) return !strlen(trim($o)); 
  else if (is_object($o)) return functionallyEmpty((array)$o);

  // If it's an array!
  foreach($o as $element) 
    if (functionallyEmpty($element)) continue; 
    else return false; 

  // all good.
  return true;
}

Example Usage:

$subject = array('', '', '');

empty($subject); // returns false
functionallyEmpty($subject); // returns true

class $Subject {
    a => '',
    b => array()
}

$theSubject = new Subject();

empty($theSubject); // returns false
functionallyEmpty($theSubject); // returns true
1
  • This doesn’t work for at least inputs containing bool(true) values. Better write it like this: pastebin.com/DnFtDWgH
    – mermshaus
    Commented Apr 14, 2017 at 22:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.