note 108264 modified in types.comparisons by nikic
| From: | nikic@php.net | Date: | Mon, 30 Nov 2015 21:15:24 +0000 |
| Subject: | note 108264 modified in types.comparisons by nikic | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-204249@lists.php.net to get a copy of this message | ||
[Editor's note: As of PHP 5.4.4 this is no longer true. Integral strings that overflow into
floating point numbers will no longer be considered equal.]
Be wary of string-comparison where both strings might be interpreted as numbers. Eg:
$x="123456789012345678901234567890"; $y="123456789012345678900000000000";
echo ($x==$y)?"equal":"not_equal"; #Prints equal !!
Both strings are getting converted to floats, then losing precision, then becoming equal :-(
Using "===" or making either of the strings non-numeric will prevent this.
[This is on a 32-bit machine, on a 64-bit, you will have to make the strings longer to see the
effect]
--was--
Be wary of string-comparison where both strings might be interpreted as numbers. Eg:
$x="123456789012345678901234567890"; $y="123456789012345678900000000000";
echo ($x==$y)?"equal":"not_equal"; #Prints equal !!
Both strings are getting converted to floats, then losing precision, then becoming equal :-(
Using "===" or making either of the strings non-numeric will prevent this.
[This is on a 32-bit machine, on a 64-bit, you will have to make the strings longer to see the
effect]
http://php.net/manual/en/types.comparisons.php