Take the State of PHP 2026 Survey →

Explicit octal integer literal notation View as Markdown

Implemented (PHP 8.1)
Authors
George Peter Banyard, girgias@php.net
Date
2020-10-20
Version
1.0
Source
wiki.php.net/rfc/explicit_octal_notation

Vote

Add support for explicit octal notation closed
Yes
33
No
0
33 voters
alcaeus Yes 2020-11-16 11:58
beberlei Yes 2020-11-13 13:25
bmajdak Yes 2020-11-11 16:29
brzuchal Yes 2020-11-11 19:06
crell Yes 2020-11-11 22:34
cschneid Yes 2020-11-11 12:40
danack Yes 2020-11-11 13:37
daverandom Yes 2020-11-12 22:05
derick Yes 2020-11-11 14:43
galvao Yes 2020-11-12 08:07
geekcom Yes 2020-11-23 02:11
girgias Yes 2020-11-11 12:24
ilutov Yes 2020-11-11 12:50
jbnahan Yes 2020-11-13 10:03
kalle Yes 2020-11-12 05:41
kocsismate Yes 2020-11-11 14:25
marcio Yes 2020-11-12 16:46
mauricio Yes 2020-11-11 17:31
narf Yes 2020-11-11 13:21
nikic Yes 2020-11-12 13:34
petk Yes 2020-11-21 14:19
pollita Yes 2020-11-11 15:51
ramsey Yes 2020-11-11 16:24
reywob Yes 2020-11-12 13:46
santiagolizardo Yes 2020-11-19 18:16
sergey Yes 2020-11-12 12:24
tandre Yes 2020-11-13 02:29
thekid Yes 2020-11-15 13:27
theodorejb Yes 2020-11-11 16:16
tiffany Yes 2020-11-13 23:53
trowski Yes 2020-11-12 16:14
twosee Yes 2020-11-18 06:45
villfa Yes 2020-11-13 15:18

Discussions on the mailing list

[RFC][VOTE] Explicit octal integer literal notation
by G. P. B. · 2 messages

RFC text

Copied from wiki.php.net, last synchronized Oct 13, 2026 11:50 UTC

Introduction

PHP's literal octal notation can lead to some confusing results such as 16 === 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.

Numeric strings in PHP are always decimal. Analogous to the example from the introduction "016" == 016 evaluates to false as (int) "016" 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, (int) "0o16" will continue to evaluate to 0.

Backward Incompatible Changes

None

Proposed PHP Version(s)

PHP 8.1.

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
  • Add a flag for the FILTER_VALIDATE_INT filter to only allow octals with the explicit octal notation.

Voting Choices

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

Vote: Add support for explicit octal notation — Yes: 33 · No: 0 (closed)

Patches and Tests

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

Language specification patch TBD.

Implementation

Merged into PHP 8.1

  1. Commit: https://git.php.net/?p=php-src.git;a=commit;h=589bdf30b2bea10172a49bcad26d44b18f192556
  2. a link to the PHP manual entry for the feature
  3. 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