Take the State of PHP 2026 Survey →

Remove hex support in numeric strings View as Markdown

Implemented (PHP 7.0)
Authors
Nikita Popov nikic@php.net
Date
2014-08-19
Source
wiki.php.net/rfc/remove_hex_support_in_numeric_strings

Vote

Remove hex support in numeric strings? closed
Yes
29
No
0
29 voters
aharvey Yes 2015-01-19 18:45
ajf Yes 2015-01-17 20:11
andi Yes 2015-01-18 01:06
bwoebi Yes 2015-01-26 21:59
datibbaw Yes 2015-01-19 23:38
dm Yes 2015-01-19 07:55
dmitry Yes 2015-01-19 08:43
dragoonis Yes 2015-01-17 20:28
galvao Yes 2015-01-26 22:14
guilhermeblanco Yes 2015-01-21 05:27
gwynne Yes 2015-01-19 04:07
irker Yes 2015-01-18 14:13
jedibc Yes 2015-01-19 14:23
jgmdev Yes 2015-01-21 17:45
kalle Yes 2015-01-20 13:23
laruence Yes 2015-01-18 02:53
leigh Yes 2015-01-19 17:39
levim Yes 2015-01-20 16:30
lstrojny Yes 2015-01-19 12:43
mbeccati Yes 2015-01-18 08:28
nikic Yes 2015-01-17 19:41
rasmus Yes 2015-01-27 07:28
reeze Yes 2015-01-19 07:49
santiagolizardo Yes 2015-01-19 14:31
stas Yes 2015-01-19 07:12
treffynnon Yes 2015-01-21 09:14
tyrael Yes 2015-01-25 19:23
yohgaki Yes 2015-01-17 23:47
zeev Yes 2015-01-26 22:50

Discussions on the mailing list

[RFC] Remove hex support in numeric strings
by Nikita Popov · 5 messages
[VOTE] Remove hex support in numeric strings
by Nikita Popov · 5 messages

RFC text

Copied from wiki.php.net, last synchronized Oct 13, 2026 11:51 UTC

Introduction

This RFC proposes to remove support for hexadecimal numers in is_numeric_string. Support for hex in this function is inconsistent with behavior in other places - in particular PHP does not detect hex numbers when performing integer or float casts.

PHP internally has two primary methods from converting strings into numbers:

The first, and most commonly used, are direct casts to the integer or float types (convert_to_long and convert_to_double). These casts do NOT support hexadecimal numbers:

var_dump((int)   "0x123"); // int(0)
var_dump((float) "0x123"); // float(0)

The second possibility is the is_numeric_string function, which will convert a string to either an integer or a float, whichever is more appropriate. This function does support hexadecimal numbers.

This leads to a discrepancy in behavior between type casts and behavior when is_numeric_string is used. Two examples of how this manifests:

is_numeric() function

$str = '0x123';
if (!is_numeric($str)) {
    throw new Exception('Not a number');
}
 
// Exception not thrown, instead wrong result is generated here:
$n = (int) $str; // 0

Loose equality comparison

var_dump('0x123' == '291'); // TRUE
 
var_dump((int) '0x123' == (int) '291'); // FALSE

Proposal

This RFC proposes to remove support for hexadecimal numbers in is_numeric_string to ensure consistent behavior across the language.

An alternative approach would be to add hexadecimal support for integer/double casts instead. However this would also imply that we should support integers of other bases supported by PHP syntax. In particular octal numbers and binary numbers should be detected as well (which is_numeric_string currently does not accept). However supporting octal numbers is not possible, because handling the string '0123' as the number 83 would be highly unexpected for end users of an application.

Generally hexadecimal strings are not often necessary and should be explicitly handled in the cases where they are. A robust way of both validating and parsing hexadecimal strings is given by FILTER_VALIDATE_INT in conjunction with FILTER_FLAG_ALLOW_HEX.

Backward Incompatible Changes

Hexadecimal numbers in strings are no longer supported in the following contexts:

  • The is_numeric() function.
  • Operands of the ==, +, -, *, /, %, **, ++ and -- operators.
  • Arguments of internal functions using l, L or d type specifiers or the convert_scalar_to_number function.
  • Probably some other fringe cases.

Hex string arguments to internal functions will throw the customary “non well formed numeric string” notice.

Patch

Patch available as PR: https://github.com/php/php-src/pull/780

Vote

As this is a language change a 2/3 majority is required.

The vote started on 2015-01-17 and ends on 2015-01-27.

Vote: Remove hex support in numeric strings? — Yes: 29 · No: 0 (closed)