Comparing strings in PHP
Interesting entry over at Greg Beaver's blog today on comparing strings. Apparently, when you try to use the following code:
var_dump('01' == '1.');
?>
It will actually return 'true', even though '01' is definitely not equal to '1.'. The problem here though is that PHP automatically converts both strings to a number, and thus 01 becomes 1 and 1. becomes 1 as well. This isn't what we want though.
Greg gives a few solutions in his blog entry, but the best one is just to always use the type-specific comparison operator (===) instead of the regular one (==). The above code would then be:
var_dump('01' === '1.');
?>
And that does print out the correct result (false).