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.


What i don’t like about `empty` is the fatal error: Can’t use method return value in write context.
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.
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: