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/20 23:46] crellrfc: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: 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 ''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 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 29: 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 35: 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 52: 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 70: 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)
Line 81: Line 91:
 [[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:remove_hex_support_in_numeric_strings|[4]]] PHP RFC: Remove hex support in numeric strings
  
  
rfc/explicit_octal_notation.1603237564.txt.gz · Last modified: 2020/10/20 23:46 by crell