Bug #69588 [Opn->Ver]: Inconsistent numeric string comparison

From: Date: Wed, 06 May 2015 20:30:35 +0000
Subject: Bug #69588 [Opn->Ver]: Inconsistent numeric string comparison
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-192535@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=69588&edit=1 ID: 69588 Updated by: cmb@php.net Reported by: markus at malkusch dot de Summary: Inconsistent numeric string comparison -Status: Open +Status: Verified Type: Bug -Package: PHP Language Specification +Package: Scripting Engine problem PHP Version: 5.6.8 Block user comment: N Private report: N New Comment: This is not so much a PHP Language Specification[1] issue, but rather a documentation problem. The manual section about "String conversions to number"[2] doesn't explicitly mention that hex strings ('0x') are allowed, but neither octal ('0') nor binary ('0b') strings. However, PHP 7 is going remove support for converting hex strings to number, see <http://3v4l.org/1gJXa>. [1] <https://github.com/php/php-langspec/> [2] <http://php.net/manual/en/language.types.string.php#language.types.string.conversion> Previous Comments: ------------------------------------------------------------------------ [2015-05-06 20:02:02] markus at malkusch dot de Description: ------------ The manual says in "Comparison Operators": > If [..] the comparison involves numerical strings, then each string is > converted to a number and the comparison performed numerically. That does explain why e.g. "10" == "0xa" evaluates to true. Now I would expect that every other representation of 10 would do the same. It doesn't for binary and octal representations. I.e "10" == "0b1010" evaluates to false, the same for "10" == "012". One could argue that numerical string is not defined. But then I would expect that a string for which is_numeric() returns true would qualify as a numerical string. is_numeric("012") does return true, but "012" == "10" not. Test script: --------------- <?php var_dump( 0b1010, "10" == "0b1010", // false 012, "10" == "012", // false 0xa, "10" == "0xa", // true 1E+1, "10" == "1E+1", // true 1e1, "10" == "1e1", // true 10.0, "10" == "10.0", // true +10, "10" == "+10" // true ); Expected result: ---------------- int(10) bool(true) int(10) bool(true) int(10) bool(true) double(10) bool(true) double(10) bool(true) double(10) bool(true) int(10) bool(true) Actual result: -------------- int(10) bool(false) int(10) bool(false) int(10) bool(true) double(10) bool(true) double(10) bool(true) double(10) bool(true) int(10) bool(true) ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=69588&edit=1

« previous php.bugs (#192535) next »