Unknown Error Unknown Error - 2 months ago 5
PHP Question

How do I check if array value is empty?

Here is my array ouput

Array
(
[1] => 1
[2] => 2
[3] =>
)


How do I know the
[3] =>
is empty?

foreach ($array as $key => $value) {
if (empty($value))
echo "$key empty <br/>";
else
echo "$key not empty <br/>";
}


My out put showing all is not empty. What is correct way to check is empty?

Answer

It works as expected, third one is empty

http://codepad.org/yBIVBHj0

Maybe try to trim its value, just in case that third value would be just a space.

foreach ($array as $key => $value) {
    $value = trim($value);
    if (empty($value))
        echo "$key empty <br/>";
    else
        echo "$key not empty <br/>";
}
Comments