More PHP type juggling, #2
I found the following gem over at Stackoverflow.com today. Given the following code, what do you think the result is?
<?php
var_dump("01a4" == "001a4");
var_dump("01e4" == "001e4");
The correct answer is:
bool(false)
bool(true)
Why?
PHP doesn’t like strings. It’s looking for any excuse it can find to treat your values as numbers. Change the hexadecimal characters in those strings slightly and suddenly PHP decides that these aren’t strings any more, they are numbers in scientific notation (PHP doesn’t care that you used quotes) and they are equivalent because leading zeros are ignored for numbers. To reinforce this point you will find that PHP also evaluates “01e4″ == “10000″ as true because these are numbers with equivalent values.