PHP RFC: Explicit octal integer literal notation
- Version: 1.0
- Date: 2020-10-20
- Author: George Peter Banyard, firstname.lastname@example.org
- 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
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
Add support for the explicit octal notation
0O for integer literals analogous to
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 , 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
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
Backward Incompatible Changes
Proposed PHP Version(s)
To Existing Extensions
Added support to the GMP extension.
Added support to the
FILTER_VALIDATE_INT filter when using the
Unaffected PHP Functionality
Implicit octal notation is unaffected.
- Deprecate the implicit octal notation.
- Support hexadecimal, octal, and binary numbers in strings
- Add a flag for the
FILTER_VALIDATE_INTfilter to only allow octals with the explicit octal notation.
Per the Voting RFC, there is a single Yes/No vote requiring a 2/3 majority for this proposal to be accepted.
Patches and Tests
GitHub patch: https://github.com/php/php-src/pull/6360
Language specification patch TBD.
Merged into PHP 8.1
- a link to the PHP manual entry for the feature
- a link to the language specification section (if any)