Page 1 of 1

Why does 0 == 'empty'?

Posted: Wed Feb 24, 2010 6:11 pm
by mrcoffee
After a few hours of frustration, I found that 0 == "empty" evaluates as true.

I can understand why 0 == "null" would be true and why 0 === null would be false, but I don't get what's happening above.

Can anyone explain this to me?

Thank you.

Re: Why does 0 == 'empty'?

Posted: Wed Feb 24, 2010 6:21 pm
by Darhazer
0 is equal to any string, that does not begin with a number.
This is because when you are comparing two different types, one of the operands is cast to the type of the another.
And when you cast a string to integer, it becomes 0 (or if it begins with numbers, it becomes the numeric part of the string)
You can read more at http://php.net/manual/en/language.opera ... arison.php

Re: Why does 0 == 'empty'?

Posted: Wed Feb 24, 2010 6:33 pm
by mrcoffee
Okay, got it now. Apparently I did not understand why 0 == 'null' :)

Thank you, that was really bothering me.