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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
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

Pull Requests

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-2024 The PHP Group
All rights reserved.
Last updated: Fri Nov 22 12:01:29 2024 UTC