Hi,
I came across this:
echo sizeof(array());
echo sizeof("");
$a = "";
var_dump( empty($a));
$a = array();
var_dump(empty($a));
So funny! How something can have a size greater than 0 but still be empty?
I think PHP is reinventing the inconsistency word.
But then let assume that empty is just making a cast in array.
$a = "";
empty($a) //true
empty((array)$a) //false
Ok so empty is big ugly switch case on type.
empty(0); //true
empty(45); //false
Wooow reinventing emptiness on number....
-- Mathieu Suen
Hello,
On Wed, Jan 13, 2010 at 2:36 PM, mathieu.suen
mathieu.suen@easyflirt.com wrote:
Hi,
I came across this:
echo sizeof(array());
echo sizeof("");
$a = "";
php.net/count:
"If var is not an array or an object with implemented Countable
interface, 1 will be returned. There is one exception, if var is NULL,
0 will be returned."
var_dump( empty($a));
$a = array();
var_dump(empty($a));
empty($var) is basically an !isset($var) || !$var
it's not related to count in anyway.
So funny! How something can have a size greater than 0 but still be empty?
I think PHP is reinventing the inconsistency word.
I think such comments are pretty useless. Try coming with a viable
solution as a patch instead.
But then let assume that empty is just making a cast in array.
If you assume wrong, you can derive all kind of madness.
$a = "";
empty($a) //true
empty((array)$a) //falseOk so empty is big ugly switch case on type.
No, it's a boolean check, with type juggling involed, see above.
empty(0); //true
empty(45); //falseWooow reinventing emptiness on number....
-- Mathieu Suen
--
--
Etienne Kneuss
http://www.colder.ch
var_dump((array)"");
Results in:
array(1) {
[0]=>
string(0) ""
}
And 'array("")' is not empty.
-- Jille
Op 13-1-2010 14:50, Etienne Kneuss schreef:
Hello,
On Wed, Jan 13, 2010 at 2:36 PM, mathieu.suen
mathieu.suen@easyflirt.com wrote:Hi,
I came across this:
echo sizeof(array());
echo sizeof("");
$a = "";php.net/count:
"If var is not an array or an object with implemented Countable
interface, 1 will be returned. There is one exception, if var is NULL,
0 will be returned."var_dump( empty($a));
$a = array();
var_dump(empty($a));empty($var) is basically an !isset($var) || !$var
it's not related to count in anyway.
So funny! How something can have a size greater than 0 but still be empty?
I think PHP is reinventing the inconsistency word.I think such comments are pretty useless. Try coming with a viable
solution as a patch instead.But then let assume that empty is just making a cast in array.
If you assume wrong, you can derive all kind of madness.
$a = "";
empty($a) //true
empty((array)$a) //falseOk so empty is big ugly switch case on type.
No, it's a boolean check, with type juggling involed, see above.
empty(0); //true
empty(45); //falseWooow reinventing emptiness on number....
-- Mathieu Suen
Etienne Kneuss wrote:
Hello,
On Wed, Jan 13, 2010 at 2:36 PM, mathieu.suen
mathieu.suen@easyflirt.com wrote:Hi,
I came across this:
echo sizeof(array());
echo sizeof("");
$a = "";php.net/count:
"If var is not an array or an object with implemented Countable
interface, 1 will be returned. There is one exception, if var is NULL,
0 will be returned."
The behavior might be documenting but it does not mean that's a good thing.
I don't see the point of allowing sizeof on a var that is not an array
or object that implement countable.
var_dump( empty($a));
$a = array();
var_dump(empty($a));empty($var) is basically an !isset($var) || !$var
it's not related to count in anyway.
So funny! How something can have a size greater than 0 but still be empty?
I think PHP is reinventing the inconsistency word.I think such comments are pretty useless. Try coming with a viable
solution as a patch instead.But then let assume that empty is just making a cast in array.
If you assume wrong, you can derive all kind of madness.
$a = "";
empty($a) //true
empty((array)$a) //falseOk so empty is big ugly switch case on type.
No, it's a boolean check, with type juggling involed, see above.
Ho yes but type juggling is pretty mush a big switch case
empty(0); //true
empty(45); //falseWooow reinventing emptiness on number....
-- Mathieu Suen
Never use empty()
I've been burned by this too many times.
It's behaviour around '0' and friends changed over the years.
Some colleagues at a former job looked at me funny when they first
heard my rant about this. Then they tracked down 3 separate bugs and
fixed them, all deriving from the use of empty().
ymmv
Hi,
I came across this:
echo sizeof(array());
echo sizeof("");
$a = "";
var_dump( empty($a));
$a = array();
var_dump(empty($a));So funny! How something can have a size greater than 0 but still be
empty?
I think PHP is reinventing the inconsistency word.But then let assume that empty is just making a cast in array.
$a = "";
empty($a) //true
empty((array)$a) //falseOk so empty is big ugly switch case on type.
empty(0); //true
empty(45); //falseWooow reinventing emptiness on number....
-- Mathieu Suen
--
--
Some people ask for gifts here.
I just want you to buy an Indie CD for yourself:
http://cdbaby.com/search/from/lynch
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...