Reply to Re: [PHP] Strange comparison behaviour

Your name:

Reply:


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]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация