php – empty()

ugh, i just spent more time than i care to admit debugging a problem. the issue is that i had the wrong understanding of what the empty() function does in PHP. i always wondered where there are empty() and isset() functions in PHP, but never bothered to look up the differences.

it turns out:

$var=0;
if (empty($var)) {
print "this is empty";
}

will print “this is empty”.

why? because empty() considers many things like an empty string, 0 (as an integer or string), NULL, FALSE, array(), and a declared variable as empty. the cases that i have a beef with is 0. if i set something to 0, it isn’t empty, it’s value is 0! i guess that’s why i should have used isset() instead. lesson learned.

Leave a Reply

Your email address will not be published.