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.

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

3 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:
        

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s