PHP - compare two diffrent type variable bug or issue -
there 2 different type of variables. while comparing both variable using == operator returns weired output. bug or other issue? surprised. here code,
$a=1000; $b='1000square'; if($a==$b){ echo "a equal b"; }else{ echo "a not equal b"; } it outputs : equal b. expected : not equal b.
can explain?
thanks!!!
there difference between == , === in php. see documentation: http://dk1.php.net/manual/en/language.operators.comparison.php
what happens in statement $a==$b php needs decide how compare $a , $b. since @ least 1 operand ($a) number, php uses numerical comparison instead of string comparison. therefore $b converted number. in php, string '1000square' converted number 1000. why code shows $a==$b true.
however, $a===$b false, since === compares type well. might operator looking for.
Comments
Post a Comment