PHP 8.1 – New octal integer notation

PHP 8.1 – New octal integer notation

hex to decimal black board

Forget about a case of checking 016 == 16 causing a false response, from now on any octal integer will have to be written in literal notation, which is 0o16 . This only applies to integer types, strings behavior are kept intact.

New 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

As of PHP 7.0, hexadecimal numbers in strings are not considered numeric, as the behavior 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.

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 change has no impact on the behavior of numeric strings. “0o16” would still be interpreted as a string and only a string. Moreover, (int) "0o16" will continue to evaluate to 0.

More about PHP 8.1

Read about all PHP 8.1 features and changes in here.

Leave a Comment