248

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?

Foreever
  • 7,099
  • 8
  • 53
  • 55
gbhall
  • 13,139
  • 9
  • 37
  • 42

12 Answers12

425

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.

blackbishop
  • 30,945
  • 11
  • 55
  • 76
ioseb
  • 16,625
  • 3
  • 33
  • 29
72

You can also check it by doing.

if(count($array) > 0)
{
    echo 'Error';
}
else
{
    echo 'No Error';
}
brenjt
  • 15,997
  • 13
  • 77
  • 118
  • 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 May 16 '14 at 13:08
  • empty() handles FALSE and isset(), so you may want empty() for that reason. – Brian Lewis Aug 21 '15 at 19:26
16

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

0xd
  • 1,891
  • 12
  • 18
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.

wrren
  • 1,281
  • 9
  • 11
8

array with zero elements converts to false

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

Eelco
  • 111
  • 1
  • 5
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.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
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)
David
  • 209
  • 1
  • 6
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)

karthikr
  • 97,368
  • 26
  • 197
  • 188
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";
   ?>
Adrian Amigues
  • 161
  • 1
  • 1
  • 7
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
  • This doesn’t work for at least inputs containing bool(true) values. Better write it like this: https://pastebin.com/DnFtDWgH – mermshaus Apr 14 '17 at 22:40
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?

Marc B
  • 356,200
  • 43
  • 426
  • 500
1
<?php
if(empty($myarray))
echo"true";
else
echo "false";
?>
Vinit
  • 1,815
  • 17
  • 38