Bug #69588 [Ver]: Inconsistent numeric string comparison
| From: | nikic@php.net | Date: | Wed, 06 May 2015 20:40:50 +0000 |
| Subject: | Bug #69588 [Ver]: Inconsistent numeric string comparison | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-192538@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: nikic@php.net
Reported by: markus at malkusch dot de
Summary: Inconsistent numeric string comparison
Status: Verified
Type: Bug
Package: Scripting Engine problem
PHP Version: 5.6.8
Block user comment: N
Private report: N
New Comment:
The PHP 7 version of the spec contains a precise definition of numeric strings: https://github.com/php/php-langspec/blob/33063d96e4c42f79c2e21c0b1eddfdf1bd9cfb46/spec/05-types.md#the-string-type
(Which no longer includes hex, as cmb already mentioned.)
Previous Comments:
------------------------------------------------------------------------
[2015-05-06 20:36:35] markus at malkusch dot de
It might also be helpful if the manual would be clear in "Comparison Operators"[1] what
qualifies as "numerical strings". It would be nice to have a consistency with is_numeric()
as well.
[1]: http://php.net/manual/en/language.operators.comparison.php
------------------------------------------------------------------------
[2015-05-06 20:30:34] cmb@php.net
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>
------------------------------------------------------------------------
[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