|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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)
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 18:00:01 2025 UTC |
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>