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.

[UPDATED 2022] Change PHP version on Ubuntu, Linux

Updated 23rd August 2022

In this post, I will show you how to switch PHP version on Ubuntu, Linux. If you are developing on Windows, follow this guide to set up Windows Sub System for Linux. What will be covered: Change PHP version for Apache2 Change PHP version for Nginx Change PHP CLI version In this example, we will

WordPress wp-admin redirects to HTTPS

Updated 18th May 2021

Does your WordPress wp-admin redirect to https rather than http? Below is a simple tutorial to stop this from happening. First, check the siteurl in the wp_options table within your database. Make sure the option_value is your site address with http:// not https://. If your siteurl is set as http:// the try the following. Open

Laravel 419 page expired after login [SOLVED]

Updated 1st January 2024

Laravel is the most popular PHP framework, dominating the space since 2014, and the chosen framework by many new PHP developers. Many new developers run into the same errors when first using the framework, the most common being a 419 Page Expired error on a form post.  What is Cross-Site Request Forgery (CSRF)? Cross-site request