PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
Oren
DevNet Resident
Posts: 1640 Joined: Fri Apr 07, 2006 5:13 am
Location: Israel
Post
by Oren » Tue Apr 03, 2007 2:41 pm
Code: Select all
var_dump((bool) '0'); // false
var_dump((bool) '00'); // true
Any good reason why ((bool) '00') === true?
P.S I feel dumb, I was sure they were both
false
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098 Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia
Post
by Chris Corbyn » Tue Apr 03, 2007 3:32 pm
I think 00 in string context will not be seen as an integer. It's like "0x00" would be the literal string. 00 is just PHP syntax for octal zero, but not in strings. A string evaluate to true.
Oren
DevNet Resident
Posts: 1640 Joined: Fri Apr 07, 2006 5:13 am
Location: Israel
Post
by Oren » Tue Apr 03, 2007 4:40 pm
Code: Select all
var_dump((bool)(int) "00"); // === true
Anyway... I'm still confused
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Tue Apr 03, 2007 5:03 pm
dreamscape
Forum Commoner
Posts: 87 Joined: Wed Jun 08, 2005 10:06 am
Contact:
Post
by dreamscape » Tue Apr 03, 2007 6:00 pm
Oren wrote: Code: Select all
var_dump((bool)(int) "00"); // === true
Anyway... I'm still confused
Actually that casts to boolean false. The string "00" casts to an integer as 0... The integer 0 casts to boolean as false.
In the case of
var_dump((bool) '00'); , the string "00" casts to boolean as true.
As it says in the link feyd gave, the only strings that cast to boolean false are an empty string and "0".
Oren
DevNet Resident
Posts: 1640 Joined: Fri Apr 07, 2006 5:13 am
Location: Israel
Post
by Oren » Wed Apr 04, 2007 2:39 am
Yes I did, more than once. It still seems a bit odd to me, but whatever...
stereofrog
Forum Contributor
Posts: 386 Joined: Mon Dec 04, 2006 6:10 am
Post
by stereofrog » Wed Apr 04, 2007 4:35 am
This is perhaps the weirdest rule in php: everything that
looks like a number is treated like a number
Code: Select all
var_dump('123' == 123); // true
var_dump('0xCafe' == 51966); // true
var_dump('0123' == 0123); // false???
This was discussed many times on the internals list and although everyone agrees this is utterly stupid, it's too late to change. The best workaround is to use explicit typecasts and strict comparisons wherever possible.