Re: weird comparsion

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

 



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

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


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux