rfc:number_format_separator

This is an old revision of the document!


PHP RFC: Number Format Separator

Introduction

Long numerical literals can be a source of poor readability in code. Take the following examples:

197823459; // what number is this?
97802345932 > 97802349532; // which number is greater?
9803458239 === 9803457239; // are these numbers equal?

These are difficult to read and difficult reason about. To ameliorate this issue, this RFC proposes the introduction of a digit separator in numerical literals. This will enable for the above examples to be rewritten as:

197_823_459; // what number is this?
97_802_345_932 > 97_802_349_532; // which number is greater?
9_803_458_239 === 9_803_457_239; // are these numbers equal?

Proposal

This RFC will add support for using the underscore character as a separator in PHP's numerical literals. The separator will be supported by all numerical types and notations in PHP.

Example:

1_000_000; // versus 1000000
3.141_592; // versus 3.141592
0x02_56_12; // versus 0x025612
0b0010_1101; // versus 0b00101101
0267_3432; // versus 02673432

Chosen Semantics

The following semantics have been chosen:

  • Leading underscores are not allowed (conflicts with constant naming conventions)
  • Trailing underscores are not allowed (not beneficial, decreases readability)
  • Adjacent underscores are not allowed (not beneficial)
  • Underscores are not allowed around the period for floats, around the 0x for hexadecimal numbers, or around the 0b for binary numbers (not beneficial, decreases readability)

Invalid usage examples:

_1; // interpreted as a constant
 
1_; // PHP Parse error:  syntax error, unexpected '_' (T_STRING) in ...
 
1__000; // PHP Parse error:  syntax error, unexpected '__000' (T_STRING) in
 
100_.0; // PHP Parse error:  syntax error, unexpected '_' (T_STRING) in...
100._01; // PHP Parse error:  syntax error, unexpected '_01' (T_STRING) in...
0x_0123; // PHP Parse error:  syntax error, unexpected 'x_0123' (T_STRING) in...
0b_0101; // PHP Parse error:  syntax error, unexpected 'b_0101' (T_STRING) in...

Other than the aforementioned rules, underscores may be freely interspersed between digits:

100_000;
10_0.0_112;
0b01_10_11_00;

Why the Underscore Character?

The underscore has been chosen because of its wide usage in other languages for formatting numerical literals.

The following languages all use an underscore:

  • Ada
  • D
  • Java
  • Julia
  • Perl
  • C#
  • Ruby
  • Elixir

Few other languages have deviated from using the underscore to separate digits. One notable language that has deviated is C++, where it could not use an underscore because of conflicts with user-defined literals (specifically in a hexadecimal context). Because PHP does not have such user-defined literals, there are no technical problems with using the underscore as a digit separator in PHP. This proposal therefore seeks to follow suite with the other languages.

Why no Support for Stringy Numerics?

This RFC does not include stringy numerics because of the BC breakage involved. It will cause the coercion rules for strings to integers to change, which may potentially have wide-ranging impacts for PHP programs. If formatting stringy integer literals is desired, then support for these can be done in the next major version of PHP.

Backward Incompatible Changes

There are no BC breaks with this feature.

Proposed PHP Version(s)

PHP 7.1

RFC Impact

To SAPIs

No impact.

To Existing Extensions

No impact.

To Opcache

No impact.

New Constants

No impact.

php.ini Defaults

No impact.

Open Issues

None so far.

Future Scope

Support for stringy numerics could be added in the next major version.

Proposed Voting Choices

A simple yes/no voting option with a 2/3 majority required.

Patches and Tests

A patch has been made - this part will be updated soon.

Implementation

After the project is implemented, this section should contain

  1. the version(s) it was merged to
  2. a link to the git commit(s)
  3. a link to the PHP manual entry for the feature

References

Previous discussion on separators for numerical literals: https://marc.info/?l=php-internals&m=142431171323037&w=2 Current discussion: to be updated...

Rejected Features

Keep this updated with features that were discussed on the mail lists.

rfc/number_format_separator.1450733510.txt.gz · Last modified: 2017/09/22 13:28 (external edit)