Reputation: 5297
Php code
$test='00000000000000000000000000000001';
if($test==1)
echo 'yes1 ';
if($test=="1")
echo 'yes2 ';
if((string)$test==1)
echo 'yes3 ';
if((string)$test=="1")
echo 'yes4 ';
if((string)$test=="00000000000000000000000000000001")
echo 'yes5 ';
why all condition are true? realy (string)$test
is equal to "1"?
but when use strcmp($test,"00000000000000000000000000000001")
this correct reutrn 0 and for strcmp($test,"1")
return -1?
why?
Upvotes: 2
Views: 206
Reputation: 22656
==
(and !=
) doesn't compare the type only the value.
Since '00001'
is a string and 1
is an int then these are converted into the same type in order to compare.
In this case '00001'
is converted to an int - 1
. 1 == 1
is true.
In order to exactly match on type you should use ===
and !==
.
Upvotes: 6
Reputation: 196
Please read about php type juggling. It is explained there. http://php.net/manual/en/language.types.type-juggling.php
Also check out comparison operators. http://php.net/manual/en/language.operators.comparison.php
Upvotes: 3