|
Posted by Richard Lynch on 05/14/05 09:25
On Fri, May 13, 2005 1:18 am, Erwin Kerk said:
> Can anyone explain me why the following code:
>
> if ("info" == 0) echo "is 0\n"; else echo "not 0\n";
>
> Results in: not 0
>
>
> Whereas:
>
> if ("inf" == 0) echo "is 0\n"; else echo "not 0\n";
>
> Results in: is 0
>
> Notice the difference: info in the first sample, inf in the second sample.
Wild Guess:
PHP is interpreting "inf" as "positive infinity" which is what you would
get if you managed to overflow a number in PHP.
Similarly, if you use "-inf" you will likely get the same "weird" result
and if you use "nan" (not a number) you may well get that "weird" result.
Bottom line: You should *NOT* be comparing random arbitrary strings to
numbers, even in PHP.
Yeah, sure, PHP will generally do the right thing and convert "123" to 123
when it needs to. But if you really want to do it right, do the
conversion yourself with:
$inf = (int) "inf";
if ($inf == 0) echo "is 0\n"; else echo "not 0\n";
--
Like Music?
http://l-i-e.com/artists.htm
[Back to original message]
|