DEV Community

Martin Jirasek
Martin Jirasek

Posted on

Human readable big numbers in PHP

You’ve probably encountered situations where you need to write out a large number, and the longer it gets, the harder it is to tell how many digits it actually has. Is 130000000 equal to 13 million, 130 million, or 1.3 billion? At first glance, it’s a mess.

$number = 130000000 // This number is 😐? 
Enter fullscreen mode Exit fullscreen mode

One alternative is to use exponential notation, which makes the magnitude clear:

$number = 130e6 // immediately obvious: 130 million 🤘 
Enter fullscreen mode Exit fullscreen mode

But for numbers like 133456115, writing it as 133.456115e6 starts to feel counterproductive.

Starting with PHP 7.4, there’s a handy new syntax: the numeric literal separator. You can sprinkle underscores into your numbers to improve readability, just like you would in a miliseconds or any other long number that you want to ready clearly:

$number = 130_000_000; // 🤩 WOW, that’s clearly 130 million! 
Enter fullscreen mode Exit fullscreen mode

The underscores don’t change the value or the type:

var_dump(130_000_000 === 130000000); // true (integer === integer) var_dump(130_000_000.000 === 130000000); // false (float !== integer) 
Enter fullscreen mode Exit fullscreen mode

See the RFC for more details:
PHP RFC

PS: JavaScript supports a similar feature!

Top comments (0)