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

Disallow Leading Underscores

Leading underscores will not enhance readability and will conflict with constant naming conventions.

_100; // a valid constant in PHP

Disallow Trailing Underscores

Trailing underscores will not enhance readability - if anything, they will decrease it.

100_<=>1_000_+_CONST_NAME; // PHP Parse error:  syntax error, unexpected '_' (T_STRING) in ...

Disallow Adjacent Underscores

Allowing for two or more underscores to be placed together will provide no further readability benefits.

1__000___000_000; // PHP Parse error:  syntax error, unexpected '__000' (T_STRING) in

Enable Underscores Between Digits Only

Underscores are not allowed around the period for floats, around the 0x for hexadecimal numbers, or around the 0b for binary numbers. This is because readability will be negatively impacted, and it doesn't really serve the purpose of a “digit separator.”

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...

Enable for Arbitrary Grouping of Digits

Underscores may be freely interspersed between arbitrary groups of digits, enabling for developers to group the digits as they see fit. For example, not all countries group digits in sets of three [1]. But also, other type notations for integers in PHP can be aptly grouped.

1_234_567; // grouped in sets of 3
123_4567; // grouped in sets of 4 (Japan, China)
10_00_00_00_000; // in India
 
// A number that will be used as bytes, grouped by bytes
0x11_22_33_44_55_66;
 
// A number that will be used as 16-bit data, grouped by word
0x1122_3344_5566;

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 exception 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. 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

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

[1]: http://www.statisticalconsultants.co.nz/blog/how-the-world-separates-its-digits.html

Rejected Features

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

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