rfc:explicit_octal_notation

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:explicit_octal_notation [2020/10/21 14:59] – Change status girgiasrfc:explicit_octal_notation [2021/01/04 20:12] (current) – Change status to implemented and specify links girgias
Line 3: Line 3:
   * Date: 2020-10-20   * Date: 2020-10-20
   * Author: George Peter Banyard, <girgias@php.net>   * Author: George Peter Banyard, <girgias@php.net>
-  * Status: Under Discussion+  * 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 ''016'' is 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 and ''0O'' when using the <php>octdec()</php> and <php>base_convert()</php> functions.+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> <code php>
 0o16 === 14; // true 0o16 === 14; // true
 0o123 === 83; // true 0o123 === 83; // true
 +
 +0O16 === 14; // true
 +0O123 === 83; // true
  
 016 === 0o16; // true 016 === 0o16; // true
 +016 === 0O16; // true
 </code> </code>
  
Line 31: Line 35:
 <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> <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>
  
-Therefore, this RFC has no impact on numeric strings.  ''"0o16"'' is still interpreted as a string and only a string.+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 37: Line 43:
  
 ===== Proposed PHP Version(s) ===== ===== Proposed PHP Version(s) =====
-Next PHP 8.x.+PHP 8.1.
  
 ===== RFC Impact ===== ===== RFC Impact =====
Line 56: Line 62:
   * Deprecate the implicit octal notation.   * Deprecate the implicit octal notation.
   * Support hexadecimal, octal, and binary numbers in strings   * 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 =====
  
 Per the Voting RFC, there is a single Yes/No vote requiring a 2/3 majority for this proposal to be accepted. Per the Voting RFC, there is a single Yes/No vote requiring a 2/3 majority for this proposal to be accepted.
Line 74: Line 81:
 ===== Implementation ===== ===== Implementation =====
  
-After the project is implemented, this section should contain  +Merged into PHP 8.1 
-  - the version(s) it was merged into +  - Commit: https://git.php.net/?p=php-src.git;a=commit;h=589bdf30b2bea10172a49bcad26d44b18f192556
-  - a link to the git commit(s)+
   - 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)
rfc/explicit_octal_notation.1603292370.txt.gz · Last modified: 2020/10/21 14:59 by girgias