php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #69588 Inconsistent numeric string comparison
Submitted: 2015-05-06 20:02 UTC Modified: 2016-03-27 20:18 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: markus at malkusch dot de Assigned:
Status: Verified Package: Scripting Engine problem
PHP Version: 5.6.8 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: markus at malkusch dot de
New email:
PHP Version: OS:

 

 [2015-05-06 20:02 UTC] 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)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-05-06 20:30 UTC] cmb@php.net
-Status: Open +Status: Verified -Package: PHP Language Specification +Package: Scripting Engine problem
 [2015-05-06 20:30 UTC] 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:36 UTC] 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:40 UTC] nikic@php.net
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.)
 [2016-03-27 20:18 UTC] nikic@php.net
-Type: Bug +Type: Documentation Problem
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC