php - Comparing 2 different values returns true -
php - Comparing 2 different values returns true -
this question has reply here:
why (0 == 'hello') homecoming true in php? 5 answersi came across while playing php
$test1 = '123abc'; $test2 = 123; var_dump($test1); echo "<br />"; var_dump($test2); echo "<br />"; $test3 = ($test1 == $test2) ? true : false; var_dump($test3);
this resulted in:
string(6) "123abc" int(123) bool(true)
could explain why $test3 came out true?
using "===" create false due comparing string int.
also notice if forcefulness (string)$test2 give me expected false, , forcing (int)$test1 returns expected true;
does mean $test3 homecoming true because php automatically converted $test1 int before comparison?
look @ the details here:
a string' == 0 evaluates true because any string converted integer when compared integer. if php can't convert string evaluated 0. 0 equal 0, equates true.
also, in the official php documentation:
to explicitly convert value integer, utilize either (int) or (integer) casts. however, in cases cast not needed, since value automatically converted if operator, function or command construction requires integer argument.
php
Comments
Post a Comment