avoiding PHP empty function

Many PHP Developers avoid using the PHP empty function. This is because many believe it should return false in some cases where it returns true.

As you can see below empty() behaves as you would expect when passing arrays and strings but confusion begins when the string "0" is returned as TRUE. And why is TRUE considered not empty but FALSE is?

What the???

This function is doing too much work. You might have to use a condition like:


$myvar = '0';
return (empty($var) && ($myvar != '0'));

Empty() returns true when evaluating:

  • "" (an empty string)
  • 0 (0 as an integer)
  • "0" (0 as a string)
  • NULL
  • FALSE
  • array() (an empty array)
  • var $var; (a variable declared, but without a value in a class)

http://au.php.net/empty




Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options