Why does PHP not recognize the sequence of digits from PHP_INT_MIN as an integer

Viewed 65

With

var_dump(PHP_INT_MIN);
//int(-9223372036854775808)

I get the number -9223372036854775808. If I assign this number to a variable, it is of the float type.

$n = -9223372036854775808;
var_dump($n);
//float(-9.2233720368548E+18)

This gives me integers:

$n = PHP_INT_MIN;
var_dump($n);
//int(-9223372036854775808)

and

$n = intval("-9223372036854775808");
var_dump($n);
//int(-9223372036854775808)

and

$n = sscanf("-9223372036854775808","%d")[0];
var_dump($n);
//int(-9223372036854775808)

Why does PHP give me a float type for -9223372036854775808 ?

1 Answers

Because the minus sign is not part of the syntax of a number in source code, it's the unary negation operator. So

$n = -9223372036854775808;

is treated like

$n = -(9223372036854775808);

But 9223372036854775808 is larger than PHP_INT_MAX, so it's parsed as a float, and then negated.

Related