Re: Strange comparison behaviour

From: Date: Sat, 14 May 2005 06:25:13 +0000
Subject: Re: Strange comparison behaviour
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-215205@lists.php.net to get a copy of this message
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

« previous php.general (#215205) next »