Re: if statement probable bug

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

 



TheI2eptile wrote:
Probably this is the wrong place to put this but I couldn't search for

not at all the wrong place, having said that the only thing probable with regard to you/your post is that you are not fully aware of the nature of data types in php and the way auto-typecasting works (in terms of precedence and conversion)... which is not surprising, most people get bitten by this at some time :-) because often when looking at the behaviour from the point of view of other langs (or as a complete beginner) the logic behind it may not seem at all logical...

I recommend searching for some more info on (auto)typecasting in php,
by the sounds of things you're capable of grokking the whys and wherefores without
much trouble :-)

the bug and what I saw in the bugs newsgroup was a bit strange and seem to be a log for the bugs forms.

So here is what I would call a bug, but maybe it's thought to be so:
<?php
$var1 =  0;
$var2 = "AS";

if($var1 == "AS"){
    echo "<br>Why is this printed";
}

int has higher precendence... therefore "AS" is converted to an int ... "AS" when converted to an int is zero... the order of the operands does not matter in this case, try the following one liner:

<?php var_dump( (0 == "AS"), ("AS" == 0) ); ?>


if(0 == "AS"){ echo "<br>At least this should not be printed"; }

same rules applies here.


if($var2 == "AS"){
echo "<br>Only this should";
}
?>
I didn't expect it. I use php 5.02 and have the zend debugger installed. Does anybody see every echo and finds this strange in a typeless language?

php is not typeless! its dynamically typed. I wonder if there are typeless langs at all :-)

btw: yes I see every echo, no I don't find it strange (anymore!)

as someone else already mentioned: use the strict equality operator (===) to
tell php not to do auto-typecasting on the vars you want to check for equality.

rgds,
Jochem



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