Reputation: 341
in php.net the following is written:
If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically. These rules also apply to the switch statement. The type conversion does not take place when the comparison is
===
or!==
as this involves comparing the type as well as the value
var_dump("10" == "1e1"); // 10 == 10 -> true
var_dump(100 == "1e2"); // 100 == 100 -> true
why in the first example it is evaluated as true but, the statement $num = (int)"1e1" ; is evaluated as 1 and not 10??? furthermore, why in the second example it is evaluated as true but the statement $num = (int)"1e2" ; is evaluated as 1 and not 100??
Upvotes: 0
Views: 193
Reputation: 3
1e1 is double.
var_dump("10" == "1e1");
// 10 (converts to real type, not int) == 10 (converts to real type, not int) -> true
My attempt to convert the numeric string (float type) to integer:
(int)'1E1'
It turns 1 instead of 100.
Upvotes: 0
Reputation: 9
Casting is not the same as equality.
Upvotes: 0
Reputation: 12017
I am not sure why (int)'1E1'
displays 1 (it probably ignores all letters and anything after), but what works perfectly for me is this:
echo '1E1'*1; //returns 10
Upvotes: 1