Re: Bug in == comparison?

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

 



hrm..

It just seemed strange as the script I was working on is 3 years old and had worked flawlessly until today.

Thanks.

- Jeff

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Jeffrey Sambells
Director of Research and Development
Zend Certified Engineer (ZCE)

We-Create Inc.
jeff@xxxxxxxxxxxx email
519.745.7374 x103 office
519.897.2552 mobile

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Get Mozilla Firefox at
http://spreadfirefox.com

On 21-Jul-06, at 12:45 PM, Adam Zey wrote:

Jeffrey Sambells wrote:
OK I have a very strange bug:
In the middle of my script I have these two lines:
var_dump($test,$test2);
echo "'$test'=='$test2' is ".($test==$test2);
which is giving:
int(0) string(6) "Points"
'0'=='Points' is 1
I understand that PHP is loose typed and automatically does type conversion but in the many years I've been using PHP (mostly v4) the comparison had always converted to 'string' and in the case above returned FALSE, not TRUE. It seems here they are comparing as integers thus 'Points' evaluates to 0 and the comparison is TRUE. I tried comparing in the reverse sequence ($test2==$test) and the same occurred. It does work as expected if I have === but the rest of the scirpt isn't type sensitive so I want NULL, 0, and empty string to still maintain equality. Any ideas why this is suddenly happening? I'm using PHP 5.1, and I realize I could use other functions such as strval() in the comparison however I've used similar logic in the past without problems.
Any help would be great.
Thanks
Jeff

No, the opposite is true; strings are always converted to integers in such comparisons, as stated in TFM:

http://www.php.net/manual/en/language.operators.comparison.php

I'll quote it here:

"If you compare an integer with a string, the string is converted to a number. If you compare two numerical strings, they are compared as integers. These rules also apply to the switch statement."

In your case, it's a bug in your code; you should be using === to also verify type, or casting $var1 to a string in your comparison.

Regards, Adam Zey.

--
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