I love empty,  because its fast and compact.

checking an array for a boolean value when a key does not exist, like here:

A:
if ( empty($foo['x']) ) echo 'no x';
this does not throw a notice that 'x' does not exist and is the fastest variant compared to these two, even if 'x' exists. I especially like the lack of ! to negate the condition.

B:
if ( !$foo['x'] ) echo 'no x';
this is simple but not faster in any case and throws a notice if it is not set.

C:
if ( !isset($foo['x']) || !$foo['x'] ) echo 'no x';
this is very correct but clumsy and slower than empty() in any case

as far as consistency is concerned I think that it behaves the same as B & C...but I may be wrong:) PHP always had a canny behaviour when converting any value to a boolean, but when I got used to it and escaped the common pitfalls, I started liking it for it's compactness and it's naturalness.

of course, an empty string is empty(), an empty array is empty() and zero is empty()...naturally. The atypical behaviour that it throws a fatal error when using it like this:
if ( empty(bar()) )

probably stems from the fact that it's really fast;) it's not very bad though...


--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to