rfc:remove_hex_support_in_numeric_strings

This is an old revision of the document!


PHP RFC: Remove hex support in numeric strings

  • Date: 2014-08-19
  • Author: Nikita Popov nikic@php.net
  • Status: Draft
  • Target version: PHP 7

Introduction

This RFC proposes to remove support for hexadecimal numers in is_numeric_string. Support for hex in this function is inconsistent with normal PHP behavior - in particular PHP does not detect hex numbers when performing integer 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 to NOT support hexadecimal decimals:

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 usual numeric string handling 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 taken here:
$n = (int) $str; // 0

Loose equality comparison

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

Proposal

Backward Incompatible Changes

Patch

Vote

rfc/remove_hex_support_in_numeric_strings.1408469833.txt.gz · Last modified: 2017/09/22 13:28 (external edit)