PHP: Empty() is the Opposite of a Boolean Variable

From the know your language department: It’s already written in the PHP manual, if we could only read it 😉 :

empty() is the opposite of (boolean) var, except that no warning is generated when the variable is not set.

So if you’re feeling a variable being empty, than this is a false and the opposite to it is a true regardless of the variable being set.

So in code:

$result = isset($var) && $var;

is the same as:

$result = !empty($var);

This means constructs like the following:

$is_ssl = isset($args['ssl']) && $args['ssl'];

must have been written by a coder that loves to type a lot. The other coder who is a bit more lazy did it this way:

$is_ssl = !empty($args['ssl']);

Well, let’s be fair. The first example has the benefit of not making use of a false false (of which a not empty could be considered) but a more straight forward true && true. So the later could be seen as more complex as a single hit.

And to be even more fair, this is about type-juggling. With php you should sit safe in the saddle when it’s about converting types from and to booleans.

Empty and Bool Side by Side

So as it was said that empty is the opposite of a boolean variable, let’s put this side by side:

Things Empty Bool
“” (an empty string) True False
0 (0 as an integer) True False
0.0 (0 as a float) True False
“0” (0 as a string) True False
NULL (incl. unset variables) True False
FALSE True False
array() (an empty array) True False
var $var; (a variable declared, but without a value in a class) True False
SimpleXML objects created from empty tags True False

Everything else is considered to be boolean true and not empty.

This entry was posted in PHP Development, Pressed, The Know Your Language Department and tagged , , . Bookmark the permalink.

5 Responses to PHP: Empty() is the Opposite of a Boolean Variable

  1. pe says:

    What i don’t like about `empty` is the fatal error: Can’t use method return value in write context.

    • hakre says:

      Try that with (bool) or isset() and you’ll get the same error. That’s not related to empty() in specify and hard to come around with in current PHP since some time.

      • pe says:

        Yeah, that is a bit odd, even the `isset`behaviour makes some sense, i would expect a dynamic evaluation.

        It seems that (bool) works on return values:

        class Klass {
                private $m;
                public function get() {
                        return $this->m;
                }
        }
        
        $klass = new Klass;
        
        /*
        empty($klass->get());
        // PHP Fatal error:  Can't use method return value in write context in test-empty.php on line 12
        */
        
        // (bool) works on return values
        ($klass->get() && true);
        
        // if (isset($klass->get()) {}
        // ^ PHP Fatal erro:
        
  2. arvindk says:

    Probably I am too late to comment on this but years ago, when I started to learn PHP, the empty($a[‘color’]) check would throw a notice if ‘color’ is not set in the array $a.

    So if someone is writing $is_ssl = isset($args[‘ssl’]) && $args[‘ssl’]; believe me that it was due to the habit they got when working with PHP in that era i.e. around 2007.

    • hakre says:

      This is interesting to read. The article is back from 2011, and at that time I already did some years of PHP and I never had the behaviour you comment on. However it might not be relevant since a couple of years.

      What perhaps makes a difference is this: https://3v4l.org/AN3kM

      7: var_dump(empty($undef[$index]));
      8:
      9: $index = 1;
      10: var_dump(empty($undef[$index]));

      For all PHP versions:

      Warning: Undefined variable $index in /in/AN3kM on line 7
      bool(true)
      bool(true)

      The suppression of the warning is not when there is/are another variable(s) involved in the expression. empty() (and isset()) suppress the warning only on the variable expression itself, not components of it.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.