[nycphp-talk] comparison with zero
Dan Cech
dcech at phpwerx.net
Fri Jul 21 11:43:59 EDT 2006
Scott Mattocks wrote:
> Dan Cech wrote:
>> Scott Mattocks wrote:
>>> A non-empty string that is converted to a boolean will be converted to
>>> TRUE. Also, the === operator converts all things to strings and then
>>> compares the string values to see if the two are equal. Therefore, "str"
>>> === 0 becomes "str" == "0" not 0 == 0.
>> Actually, no.
>>
>> The === operator does not do any conversion at all, the 2 arguments must
>> have the same type and the same value for === to return true.
>
> Of course I can't find the page where I read this but I know that I have
> seen many times, "=== compares the string representation of the two
> variables."
>
> Which is why you get different results for "1" == "01" and "1" === "01".
>
> I will keep looking for the page.
If you think about it this cannot be correct, as there are many examples
of variables which are not identical though the results of converting
them to a string are.
The reason you get different results for "1" == "01" and "1" === "01" is
that the first compares them as numbers (because they are both numeric
strings), while the second compares them as strings.
Dan
More information about the talk
mailing list