Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
3  
Works correctly for me in PHP 5.3 on Windows - I get Error – Pekka 웃 Nov 30 '11 at 16:05
stackoverflow.com/questions/4139301/… check answer no 2 – Nick Nov 30 '11 at 16:06
Is the output of this code "Error" or "No errors"? – yankitwizzy Nov 30 '11 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"? – liquorvicar Nov 30 '11 at 16:11
I get Error too. I wanted it so that if $error['something'] = true, then only will $error return true. – gbhall Nov 30 '11 at 16:16

9 Answers

up vote 22 down vote accepted

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.

share|improve this answer
Thank you. I think I may have worded my question badly, but you understood what I meant and provided the solution. – gbhall Nov 30 '11 at 16:19

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.

share|improve this answer

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

share|improve this answer

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)
share|improve this answer

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.

share|improve this answer

You can also check it by doing.

if(count($array) > 0)
{
    echo 'Error';
}
else
{
    echo 'No Error';
}
share|improve this answer

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?

share|improve this answer
<?php
if(empty($myarray))
echo"true";
else
echo "false";
?>
share|improve this answer

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)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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