rfc:explicit_octal_notation

Differences

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

Link to this comparison view

Next revision
Previous revision
rfc:explicit_octal_notation [2020/10/20 22:32] – created girgiasrfc:explicit_octal_notation [2021/01/04 20:12] (current) – Change status to implemented and specify links girgias
Line 1: Line 1:
 ====== PHP RFC: Explicit octal integer literal notation ====== ====== PHP RFC: Explicit octal integer literal notation ======
-  * Version: 0.9+  * Version: 1.0
   * Date: 2020-10-20   * Date: 2020-10-20
   * Author: George Peter Banyard, <girgias@php.net>   * Author: George Peter Banyard, <girgias@php.net>
-  * Status: Draft+  * Status: Implemented (https://git.php.net/?p=php-src.git;a=commit;h=589bdf30b2bea10172a49bcad26d44b18f192556)
   * First Published at: https://wiki.php.net/rfc/explicit_octal_notation   * First Published at: https://wiki.php.net/rfc/explicit_octal_notation
  
 ===== Introduction ===== ===== Introduction =====
-PHP's literal octal notation can lead to some confusing results such as <php>"016" == 016</php> evaluating to false. This is because "016is evaluated as an octal integer and resolves to ''14''.+ 
 +PHP's literal octal notation can lead to some confusing results such as <php>16 === 016</php> evaluating to false. This is because ''016'' is evaluated as an octal integer and resolves to ''14''.
  
 This convention for octal integers is well established and followed by many programming languages (Java, C, C#, Golang, Haskell, and more). However, Python, JavaScript, and Rust [[https://docs.python.org/3/reference/lexical_analysis.html#integer-literals|[1]]][[https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Numbers_and_dates|[2]]][[https://doc.rust-lang.org/rust-by-example/primitives/literals.html|[3]]] only accept or support an explicit octal notation ''0o''. This convention for octal integers is well established and followed by many programming languages (Java, C, C#, Golang, Haskell, and more). However, Python, JavaScript, and Rust [[https://docs.python.org/3/reference/lexical_analysis.html#integer-literals|[1]]][[https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Numbers_and_dates|[2]]][[https://doc.rust-lang.org/rust-by-example/primitives/literals.html|[3]]] only accept or support an explicit octal notation ''0o''.
 +
 +Surprisingly PHP already has support for this notation when using the <php>octdec()</php> and <php>base_convert()</php> functions.
  
 ===== Proposal ===== ===== Proposal =====
-Add support for the explicit octal notation ''0o'' for integer literals analogous to ''0x'' and ''0b'' for hexadecimal and binary.+ 
 +Add support for the explicit octal notation ''0o''/''0O'' for integer literals analogous to ''0x''/''0X'' and ''0b''/''0B'' for hexadecimal and binary
 + 
 +<code php> 
 +0o16 === 14; // true 
 +0o123 === 83; // true 
 + 
 +0O16 === 14; // true 
 +0O123 === 83; // true 
 + 
 +016 === 0o16; // true 
 +016 === 0O16; // true 
 +</code> 
 + 
 +===== Behaviour of numeric strings ===== 
 + 
 +As of PHP 7.0, hexadecimal numbers in strings are not considered numeric [[rfc:remove_hex_support_in_numeric_strings|[4]]], as the behaviour was inconsistent with type casting.  Adding complete support for hex numbers in strings was rejected because adding it for other numeric types would be complex and confusing.  In particular: 
 + 
 +<blockquote>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.</blockquote> 
 + 
 +Numeric strings in PHP are //always// decimal. Analogous to the example from the introduction <php>"016" == 016</php> evaluates to false as <php>(int) "016"</php> evaluates to ''16''
 + 
 +This RFC has no impact on the behaviour of numeric strings.  ''"0o16"'' would still be interpreted as a string and only a string. Moreover, <php>(int) "0o16"</php> will continue to evaluate to ''0''.
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
Line 18: Line 43:
  
 ===== Proposed PHP Version(s) ===== ===== Proposed PHP Version(s) =====
-Next PHP 8.x.+PHP 8.1.
  
 ===== RFC Impact ===== ===== RFC Impact =====
 ==== To Existing Extensions ==== ==== To Existing Extensions ====
 Added support to the GMP extension. Added support to the GMP extension.
 +
 +Added support to the <php>FILTER_VALIDATE_INT</php> filter when using the <php>FILTER_FLAG_ALLOW_OCTAL</php> flag
  
 ==== To Opcache ==== ==== To Opcache ====
Line 28: Line 55:
  
 ===== Unaffected PHP Functionality ===== ===== Unaffected PHP Functionality =====
 +
 Implicit octal notation is unaffected. Implicit octal notation is unaffected.
  
 ===== Future Scope ===== ===== Future Scope =====
 +
   * Deprecate the implicit octal notation.   * Deprecate the implicit octal notation.
 +  * Support hexadecimal, octal, and binary numbers in strings
 +  * Add a flag for the <php>FILTER_VALIDATE_INT</php> filter to only allow octals with the explicit octal notation.
  
-===== Proposed Voting Choices ===== +===== Voting Choices ===== 
-Include these so readers know where you are heading and can discuss the proposed voting options.+ 
 +Per the Voting RFC, there is a single Yes/No vote requiring a 2/3 majority for this proposal to be accepted. 
 + 
 +<doodle title="Add support for explicit octal notation" auth="girgias" voteType="single" closed="true"> 
 +   * Yes 
 +   * No 
 +</doodle>
  
 ===== Patches and Tests ===== ===== Patches and Tests =====
 +
 GitHub patch: https://github.com/php/php-src/pull/6360 GitHub patch: https://github.com/php/php-src/pull/6360
  
Line 42: Line 80:
  
 ===== Implementation ===== ===== Implementation =====
-After the project is implemented, this section should contain  + 
-  - the version(s) it was merged into +Merged into PHP 8.1 
-  - a link to the git commit(s)+  - Commit: https://git.php.net/?p=php-src.git;a=commit;h=589bdf30b2bea10172a49bcad26d44b18f192556
   - a link to the PHP manual entry for the feature   - a link to the PHP manual entry for the feature
   - a link to the language specification section (if any)   - a link to the language specification section (if any)
  
 ===== References ===== ===== References =====
 +
 [[https://docs.python.org/3/reference/lexical_analysis.html#integer-literals|[1]]] Python language reference about integer literals \\ [[https://docs.python.org/3/reference/lexical_analysis.html#integer-literals|[1]]] Python language reference about integer literals \\
 [[https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Numbers_and_dates|[2]]] JavaScript language references about numbers \\ [[https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Numbers_and_dates|[2]]] JavaScript language references about numbers \\
-[[https://doc.rust-lang.org/rust-by-example/primitives/literals.html|[3]]] Rust language reference about literals+[[https://doc.rust-lang.org/rust-by-example/primitives/literals.html|[3]]] Rust language reference about literals \\ 
 +[[rfc:remove_hex_support_in_numeric_strings|[4]]] PHP RFC: Remove hex support in numeric strings 
  
rfc/explicit_octal_notation.1603233174.txt.gz · Last modified: 2020/10/20 22:32 by girgias