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 Commented Nov 30, 2011 at 16:05
  • stackoverflow.com/questions/4139301/… check answer no 2 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. Commented Nov 30, 2011 at 16:16

12 Answers 12

426

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.

Sign up to request clarification or add additional context in comments.

4 Comments

array_filter($input) also removes empty arrays from $input.
This could be trimmed down to if (array_filter($errors)) {}
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.)
or array_sum(array_values($errors)) would just tell you how many errors you have.
72

You can also check it by doing.

if(count($array) > 0)
{
    echo 'Error';
}
else
{
    echo 'No Error';
}

2 Comments

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.
empty() handles FALSE and isset(), so you may want empty() for that reason.
16

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

3 Comments

sizeof is an alias of count.
Reminder: sizeof is on the deprecation list, see wiki.php.net/rfc/deprecations_php_7_1
In this case, count (or sizeof) would return two, due to the two values which are set to false.
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.

Comments

8

array with zero elements converts to false

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

Comments

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.

Comments

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 Comment

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 Comment

This is a best way of array empty checking. Or you can use $set_errors = array_keys( $errors, true );
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";
   ?>

Comments

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?

Comments

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

Comments

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 Comment

This doesn’t work for at least inputs containing bool(true) values. Better write it like this: pastebin.com/DnFtDWgH

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.