rfc:explicit_octal_notation

This is an old revision of the document!


PHP RFC: Explicit octal integer literal notation

Introduction

PHP's literal octal notation can lead to some confusing results such as "016" == 016 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 [1][2][3] only accept or support an explicit octal notation 0o.

Surprisingly PHP already has support for this notation when using the octdec() and base_convert() functions.

Proposal

Add support for the explicit octal notation 0o/0O for integer literals analogous to 0x/0X and 0b/0B for hexadecimal and binary.

0o16 === 14; // true
0o123 === 83; // true
 
0O16 === 14; // true
0O123 === 83; // true
 
016 === 0o16; // true
016 === 0O16; // true

Behaviour of numeric strings

As of PHP 7.0, hexadecimal numbers in strings are not considered numeric [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:

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.

Therefore, this RFC has no impact on numeric strings. “0o16” is still interpreted as a string and only a string.

Backward Incompatible Changes

None

Proposed PHP Version(s)

Next PHP 8.x.

RFC Impact

To Existing Extensions

Added support to the GMP extension.

Added support to the FILTER_VALIDATE_INT filter when using the FILTER_FLAG_ALLOW_OCTAL flag

To Opcache

None

Unaffected PHP Functionality

Implicit octal notation is unaffected.

Future Scope

  • Deprecate the implicit octal notation.
  • Support hexadecimal, octal, and binary numbers in strings

Proposed Voting Choices

Per the Voting RFC, there is a single Yes/No vote requiring a 2/3 majority for this proposal to be accepted.

Add support for explicit octal notation
Real name Yes No
alcaeus (alcaeus)  
beberlei (beberlei)  
bmajdak (bmajdak)  
brzuchal (brzuchal)  
crell (crell)  
cschneid (cschneid)  
danack (danack)  
daverandom (daverandom)  
derick (derick)  
galvao (galvao)  
geekcom (geekcom)  
girgias (girgias)  
ilutov (ilutov)  
jbnahan (jbnahan)  
kalle (kalle)  
kocsismate (kocsismate)  
marcio (marcio)  
mauricio (mauricio)  
narf (narf)  
nikic (nikic)  
petk (petk)  
pollita (pollita)  
ramsey (ramsey)  
reywob (reywob)  
santiagolizardo (santiagolizardo)  
sergey (sergey)  
tandre (tandre)  
thekid (thekid)  
theodorejb (theodorejb)  
tiffany (tiffany)  
trowski (trowski)  
twosee (twosee)  
villfa (villfa)  
Final result: 33 0
This poll has been closed.

Patches and Tests

GitHub patch: https://github.com/php/php-src/pull/6360

Language specification patch TBD.

Implementation

After the project is implemented, this section should contain

  1. the version(s) it was merged into
  2. a link to the git commit(s)
  3. a link to the PHP manual entry for the feature
  4. a link to the language specification section (if any)

References

[1] Python language reference about integer literals
[2] JavaScript language references about numbers
[3] Rust language reference about literals
[4] PHP RFC: Remove hex support in numeric strings

rfc/explicit_octal_notation.1604738024.txt.gz · Last modified: 2020/11/07 08:33 by girgias