Skip to content

Commit f74109d

Browse files
theodorejbnikic
authored andcommitted
Implement numeric literal separators
RFC: https://wiki.php.net/rfc/numeric_literal_separator
1 parent ec77cca commit f74109d

11 files changed

+197
-28
lines changed

UPGRADING

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,15 @@ PHP 7.4 UPGRADE NOTES
186186

187187
RFC: https://wiki.php.net/rfc/spread_operator_for_array
188188

189+
. Added support for underscore separators in numeric literals. Some examples:
190+
191+
6.674_083e-11; // float
192+
299_792_458; // decimal
193+
0xCAFE_F00D; // hexadecimal
194+
0b0101_1111; // binary
195+
196+
RFC: https://wiki.php.net/rfc/numeric_literal_separator
197+
189198
. Support for WeakReferences has been added.
190199
RFC: https://wiki.php.net/rfc/weakrefs
191200

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
Valid use of numeric literal separator
3+
--FILE--
4+
<?php
5+
var_dump(299_792_458 === 299792458);
6+
var_dump(135_00 === 13500);
7+
var_dump(96_485.332_12 === 96485.33212);
8+
var_dump(6.626_070_15e-34 === 6.62607015e-34);
9+
var_dump(6.674_083e-11 === 6.674083e-11);
10+
var_dump(0xCAFE_F00D === 0xCAFEF00D);
11+
var_dump(0x54_4A_42 === 0x544A42);
12+
var_dump(0b0101_1111 === 0b01011111);
13+
var_dump(0b01_0000_10 === 0b01000010);
14+
var_dump(0137_041 === 0137041);
15+
var_dump(0_124 === 0124);
16+
--EXPECT--
17+
bool(true)
18+
bool(true)
19+
bool(true)
20+
bool(true)
21+
bool(true)
22+
bool(true)
23+
bool(true)
24+
bool(true)
25+
bool(true)
26+
bool(true)
27+
bool(true)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
--TEST--
2+
Invalid use: trailing underscore
3+
--FILE--
4+
<?php
5+
100_;
6+
--EXPECTF--
7+
Parse error: syntax error, unexpected '_' (T_STRING) in %s on line %d
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
--TEST--
2+
Invalid use: adjacent underscores
3+
--FILE--
4+
<?php
5+
10__0;
6+
--EXPECTF--
7+
Parse error: syntax error, unexpected '__0' (T_STRING) in %s on line %d
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
--TEST--
2+
Invalid use: underscore left of period
3+
--FILE--
4+
<?php
5+
100_.0;
6+
--EXPECTF--
7+
Parse error: syntax error, unexpected '_' (T_STRING) in %s on line %d
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
--TEST--
2+
Invalid use: underscore right of period
3+
--FILE--
4+
<?php
5+
100._0;
6+
--EXPECTF--
7+
Parse error: syntax error, unexpected '_0' (T_STRING) in %s on line %d
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
--TEST--
2+
Invalid use: underscore next to 0x
3+
--FILE--
4+
<?php
5+
0x_0123;
6+
--EXPECTF--
7+
Parse error: syntax error, unexpected 'x_0123' (T_STRING) in %s on line %d
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
--TEST--
2+
Invalid use: underscore next to 0b
3+
--FILE--
4+
<?php
5+
0b_0101;
6+
--EXPECTF--
7+
Parse error: syntax error, unexpected 'b_0101' (T_STRING) in %s on line %d
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
--TEST--
2+
Invalid use: underscore left of e
3+
--FILE--
4+
<?php
5+
1_e2;
6+
--EXPECTF--
7+
Parse error: syntax error, unexpected '_e2' (T_STRING) in %s on line %d
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
--TEST--
2+
Invalid use: underscore right of e
3+
--FILE--
4+
<?php
5+
1e_2;
6+
--EXPECTF--
7+
Parse error: syntax error, unexpected 'e_2' (T_STRING) in %s on line %d

0 commit comments

Comments
 (0)