5 ms·
Not entirely true. PHP definitely tries to perform a numeric comparison on two strings if it thinks they are both numerical strings, but this behaviour changed
by DCoder 13y ago
Not entirely true. PHP definitely tries to perform a numeric comparison on two strings if it thinks they are both numerical strings, but this behaviour changed in PHP 5.4 [0]. heron87's scenario was reported as a bug [1], changed in PHP 5.4 and caused new problems [2].
See the string comparison routine in 5.3 [3] and 5.4 [4]. The new implementation still inspects the strings to see if they are numeric and then tries to do a numeric equality check rather than string equality check, leading to amusing tricks like
var_dump("0xFF" == "255.0"); // true!
But if both values overflow the long type and their double representations are equal, the new implementation falls back to actual string equality. A quick check at [5] shows that both values heron87 compared have identical double representations, assuming (sizeof double == 8), so the new representation detects the long overflow, then it sees that double representations are also identical, and then it returns to string equality rather than numeric equality. The old implementation did not consider long overflow and ended up comparing the numeric values.
[0] http://3v4l.org/a0nKq http://3v4l.org/a0nKq
[1] https://bugs.php.net/bug.php?id=54547 https://bugs.php.net/bug.php?id=54547
[2] https://bugs.php.net/bug.php?id=62097 https://bugs.php.net/bug.php?id=62097
[3] https://github.com/php/php-src/blob/PHP-5.3/Zend/zend_operators.c#L1992 https://github.com/php/php-src/blob/PHP-5.3/Zend/zend_operat...
[4] https://github.com/php/php-src/blob/5.4/Zend/zend_operators.c#L2039 https://github.com/php/php-src/blob/5.4/Zend/zend_operators....
[5] http://babbage.cs.qc.cuny.edu/IEEE-754/index.xhtml http://babbage.cs.qc.cuny.edu/IEEE-754/index.xhtml