Syntax
Number literals
Integers and floats are written the way PHP writes them. Rules shared by both:
- A number may be prefixed by a sign: a minus (
-) makes it negative, a plus (+) changes nothing. Whitespace may stand between the sign and the number (- 1is-1). - An underscore (
_) separates digits: it stands between two digits and nowhere else, so it neither leads, nor trails, nor doubles.
Integers
Section titled “Integers”A decimal integer is a run of digits without a leading zero
([1-9][0-9]*). Other notations are prefixed and described below.
A number written with a separator.
42_04
resultTypeLang\Type\Literal\IntLiteralNode
A separator cannot trail a number.
42_
errorUnexpectedTokenException Unexpected "_"
Two separators in a row are not allowed.
4__2
errorUnexpectedTokenException Unexpected "__2"
Binary
Section titled “Binary”Prefixed with 0b or 0B, written in the digits 0 and 1.
The number 173.
0b1010_1101
resultTypeLang\Type\Literal\IntLiteralNode
The number -10, the prefix is case-insensitive.
-0B1010
resultTypeLang\Type\Literal\IntLiteralNode
A binary number contains 0 and 1 only.
0b101042
errorUnexpectedTokenException Unexpected "42"
Prefixed with 0o or 0O, written in the digits 0 to 7. For
compatibility with older PHP, a leading 0 alone is an octal prefix too.
The number 34.
0o42
resultTypeLang\Type\Literal\IntLiteralNode
Also 34, with the legacy prefix.
042
resultTypeLang\Type\Literal\IntLiteralNode
The number 2195, the prefix is case-insensitive.
0O42_23
resultTypeLang\Type\Literal\IntLiteralNode
An octal number contains the digits 0 to 7 only.
0o4281
errorUnexpectedTokenException Unexpected "81"
A leading 0 makes a number octal, so 8 cannot follow it.
08
errorUnexpectedTokenException Unexpected "8"
Hexadecimal
Section titled “Hexadecimal”Prefixed with 0x or 0X, written in the digits 0 to 9 and the letters
A to F in any case.
The number 57005.
0xDEAD
resultTypeLang\Type\Literal\IntLiteralNode
The number 3735928559, the prefix and the letters are case-insensitive.
0XDead_Beef
resultTypeLang\Type\Literal\IntLiteralNode
H and L are not hexadecimal digits.
0xHELL
errorUnexpectedTokenException Unexpected "xHELL"
Floats
Section titled “Floats”A float is two runs of digits around a dot ([0-9]+\.[0-9]+), either run may
be omitted, but not both. Every run, the exponent included, accepts the
separator.
0.9
resultTypeLang\Type\Literal\FloatLiteralNode
The same as 0.9.
.9
resultTypeLang\Type\Literal\FloatLiteralNode
The same as 1.0.
1.
resultTypeLang\Type\Literal\FloatLiteralNode
-0.9
resultTypeLang\Type\Literal\FloatLiteralNode
Separators in every part, the same as 23.45e-67.
2_3.4_5e-6_7
resultTypeLang\Type\Literal\FloatLiteralNode
Both runs of digits cannot be omitted.
.
errorUnrecognizedSyntaxException Unrecognized syntax "."
A float contains decimal digits only.
0.0A
errorUnexpectedTokenException Unexpected "A"
Scientific notation
Section titled “Scientific notation”A number followed by e (or E) and a decimal exponent with an optional sign.
The number 1000.0.
10e2
resultTypeLang\Type\Literal\FloatLiteralNode
The number 0.1, the E is case-insensitive.
10E-2
resultTypeLang\Type\Literal\FloatLiteralNode
The number 1000.0, a plus changes nothing.
10e+2
resultTypeLang\Type\Literal\FloatLiteralNode
The exponent is decimal.
10e-F
errorUnexpectedTokenException Unexpected "e-F"