# RFC: Explicit octal integer literal notation

- Status: Implemented (PHP 8.1)
- Authors: George Peter Banyard, girgias@php.net
- Date: 2020-10-20
- Version: 1.0
- Original: https://wiki.php.net/rfc/explicit_octal_notation
- Page: https://externals.io/rfc/explicit_octal_notation
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

## Votes

### Add support for explicit octal notation (closed)

Yes: 33 · No: 0

Voters: alcaeus (Yes), beberlei (Yes), bmajdak (Yes), brzuchal (Yes), crell (Yes), cschneid (Yes), danack (Yes), daverandom (Yes), derick (Yes), galvao (Yes), geekcom (Yes), girgias (Yes), ilutov (Yes), jbnahan (Yes), kalle (Yes), kocsismate (Yes), marcio (Yes), mauricio (Yes), narf (Yes), nikic (Yes), petk (Yes), pollita (Yes), ramsey (Yes), reywob (Yes), santiagolizardo (Yes), sergey (Yes), tandre (Yes), thekid (Yes), theodorejb (Yes), tiffany (Yes), trowski (Yes), twosee (Yes), villfa (Yes)

## Discussions on the mailing list

- [\[RFC\] Add support for explicit octal notation for integer literals](https://externals.io/message/112090.md) — started on 2020-10-21, 5 messages
- [\[RFC\]\[VOTE\] Explicit octal integer literal notation](https://externals.io/message/112221.md) — started on 2020-11-11, 2 messages

---

## 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\]](https://docs.python.org/3/reference/lexical_analysis.html#integer-literals)[\[2\]](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Numbers_and_dates)[\[3\]](https://doc.rust-lang.org/rust-by-example/primitives/literals.html) 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.

```php
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\]](https://externals.io/rfc/remove_hex_support_in_numeric_strings), 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\]](https://docs.python.org/3/reference/lexical_analysis.html#integer-literals) Python language reference about integer literals 
[\[2\]](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Numbers_and_dates) JavaScript language references about numbers 
[\[3\]](https://doc.rust-lang.org/rust-by-example/primitives/literals.html) Rust language reference about literals 
[\[4\]](https://externals.io/rfc/remove_hex_support_in_numeric_strings) PHP RFC: Remove hex support in numeric strings
