rfc:remove_hex_support_in_numeric_strings

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
rfc:remove_hex_support_in_numeric_strings [2014/08/19 18:06] nikicrfc:remove_hex_support_in_numeric_strings [2017/09/22 13:28] (current) – external edit 127.0.0.1
Line 2: Line 2:
   * Date: 2014-08-19   * Date: 2014-08-19
   * Author: Nikita Popov <nikic@php.net>   * Author: Nikita Popov <nikic@php.net>
-  * Status: Draft +  * Status: Implemented (in PHP 7)
-  * Target version: PHP 7+
  
 ===== Introduction ===== ===== 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.+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: 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:+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:
  
 <code php> <code php>
Line 22: Line 21:
 This leads to a discrepancy in behavior between type casts and behavior when ''is_numeric_string'' is used. Two examples of how this manifests: 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 ===+=== is_numeric() function ===
  
 <code php> <code php>
Line 30: Line 29:
 } }
  
-// Exception not thrown, instead wrong result is taken here:+// Exception not thrown, instead wrong result is generated here:
 $n = (int) $str; // 0 $n = (int) $str; // 0
 </code> </code>
Line 39: Line 38:
 var_dump('0x123' == '291'); // TRUE var_dump('0x123' == '291'); // TRUE
  
-var_dump((int) '0x123' == (int) '291')); // FALSE+var_dump((int) '0x123' == (int) '291'); // FALSE
 </code> </code>
  
 ===== Proposal ===== ===== Proposal =====
  
-This RFC proposes to remove support for hexadecimal numbers of ''is_numeric_string'' to ensure consistent behavior across the language.+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 even ''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.+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''. 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''.
Line 54: Line 53:
 Hexadecimal numbers in strings are no longer supported in the following contexts: Hexadecimal numbers in strings are no longer supported in the following contexts:
  
-  * The ''is_numeric'' function.+  * The ''is_numeric()'' function.
   * Operands of the ''=='', ''+'', ''-'', ''*'', ''/'', ''%'', ''**'', ''++'' and ''%%--%%'' operators.   * Operands of the ''=='', ''+'', ''-'', ''*'', ''/'', ''%'', ''**'', ''++'' and ''%%--%%'' operators.
   * Arguments of internal functions using ''l'', ''L'' or ''d'' type specifiers or the ''convert_scalar_to_number'' function.   * 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. Hex string arguments to internal functions will throw the customary "non well formed numeric string" notice.
Line 62: Line 62:
 ===== Patch ===== ===== Patch =====
  
-Patch available as PR: https://wiki.php.net/rfc/remove_hex_support_in_numeric_strings+Patch available as PR: https://github.com/php/php-src/pull/780
  
 ===== Vote ===== ===== 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.
 +
 +<doodle title="Remove hex support in numeric strings?" auth="nikic" voteType="single" closed="true">
 +   * Yes
 +   * No
 +</doodle>
  
rfc/remove_hex_support_in_numeric_strings.1408471571.txt.gz · Last modified: 2017/09/22 13:28 (external edit)