The numeric literal separator will be finally added to PHP 7.4, after that RFC vote finished on 13th June with 33 Yes against 11 No. The new RFC succeeded to make this new feature available, after if a similar RFC failed three years ago. The problem is that human eyes are not optimized for quickly parsing long sequences of digits. Thus, a lack of visual separators makes it take longer to read and debug code, and can lead to unintended mistakes. That’s where numeric literal separator can help :

1000000000;   // Is this a billion? 100 million? 10 billion?
‪107925284.88;‬ // What scale or power of 10 is this?

Additionally, without a visual separator numeric literals fail to convey any additional information, such as whether a financial quantity is stored in cents:

$discount = 13500; // Is this 13,500? Or 135, because it's in cents?

The proposal which is already implemented for 7.4 aims to enable improved code readability by supporting an underscore in numeric literals to visually separate groups of digits. The lines before become like this :

$threshold = 1_000_000_000;  // a billion!
$testValue = ‪107_925_284.88; // scale is hundreds of millions
$discount = 135_00;          // $135, stored as cents

“This RFC has been quite controversial on the mailing list and Reddit as people voiced their negative opinion about it.” said Benjamin Eberlei, CEO Tideways, in the RFC acceptance email, he added “Personally I think it is quite great and many other languages have numeric separators, with Java, Python, Ruby and Rust having an underscore as well.”

I think also that it’s a simple but great and very valuable feature that will improve the readability of your code. PHP is getting better, and with the first alpha release of 7.4 the countdown started for the next generation of PHP.

LEAVE A REPLY

Please enter your comment!
Please enter your name here