PHP Numeric Literal Separator

By Dillon Smart · · · 0 Comments

First introduced in PHP 7.4, the Numeric Literal Separator in PHP makes large integers within your code easy to read.

Naturally, we find it easier to read number which are grouped, for example, 1,650,000.

Using the numeric literal separator in PHP

Since PHP 7.4, we can now use numbers like this:

$number = 1_650_000;

echo $number; // 1650000

Adding an underscore between digits in a numeric literal will not change its value. The underscores are stripped out during the lexing stage, so the runtime is not affected.

I wrote an post which explains more about the Lexing stage in PHP.

var_dump(1_000_000); // int(1000000)

The numeric literal separator can be used with all numeric literal notations supported in PHP.

  • Float
  • Decimal
  • Hexadecimal
  • Binary
  • Octal

Restrictions with the numeric literal separator in PHP

The only restriction with the numeric literal separator is that each underscore must directly between wo digits.

Here is a list of examples which would throw a Parse error: syntax error.

100_;       // trailing
1__1;       // next to underscore
1_.0; 1._0; // next to decimal point
0x_123;     // next to x
0b_101;     // next to b
1_e2; 1e_2; // next to e

Learn about new features coming in PHP 8.3.

PHP

0 Comment

Was this helpful? Leave a comment!

This site uses Akismet to reduce spam. Learn how your comment data is processed.

What is PHP?

Updated 1st August 2022

PHP (PHP: Hypertext Preprocessor) is an open-source, general-purpose, and server-side scripting language widely used in web development. PHP is used to manage databases, dynamic content, session tracking, and much more. Let’s answer the question “What is PHP?”. What is PHP used for? Since its first release in 1995, PHP has been seen used in many

Find php.ini on Apple Silicon installed with Homebrew

Updated 1st October 2023

Are you having trouble finding your php.ini file which was installed locally with Homebrew? Packages installed with Homebrew can be installed in different locations making it hard to find, especially if you are on new Apple Mac running Apple’s Silicon chips. Here, I will show you home to locate your locally installed php.ini file installed

Pixel Tracking in PHP

Updated 16th August 2022

When creating email campaigns, it’s important to know how many recipients actually opened your email. This information can help you determine if your efforts are working or if it’s time to change your strategy. Most mail champaign tools such as Mailchimp and SendInBlue use a technique called Pixel Tracking. In this post, you will learn