Re: Strange comparison behaviour

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux