Re: weird comparsion

From: Date: Thu, 03 May 2007 15:15:00 +0000
Subject: Re: weird comparsion
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-43542@lists.php.net to get a copy of this message
Ford, Mike wrote:
On 03 May 2007 12:30, OKi98 wrote:
I know about identity operator (===) but with == operator 0 is false and "foo" is true
No, that's not correct. , try this:
$foo=0; $bar="bar"; if ($foo) echo("$foo is true, "); else echo("$foo is false, "); if ($bar) echo("$bar is true, "); else echo("$bar is false, "); if ($foo==$bar) echo("$foo==$bar"); returns "0 is false, bar is true, 0==$bar"
That's because you've got loads of implicit type conversions going on there, so you're not comparing like with like. For if ($foo) ... and if ($bar) ...: within the context of the if(), both $foo and $bar are implicitly converted to Boolean: - (bool)0 is FALSE - (bool)"any non-empty() string" is TRUE On the other hand, for if ($foo==$bar) ...: in the context of the == comparison, $bar is converted to a number, and any string not beginning with a numeric character converts to numeric zero -- so you get a comparison of zero with zero, which is, of course, TRUE! oh I didnt know that, I thought the number will be converted into a string. Thanks alot.
I have one more question but I ask in separate thread :) Oki98

« previous php.db (#43542) next »