I am running PHP 7.0.8 (VC14 x64 Thread Safe version) on Windows 7.
echo PHP_INT_MAX;
shows 9223372036854775807
, but this doesn't actually seem to be correct. According to the PHP documentation,
If PHP encounters a number beyond the bounds of the integer type, it will be interpreted as a float instead.
but when I run the code from the documentation that demonstrates integer overflow on a 32-bit system, that's not what happens.
$large_number = 2147483647;
var_dump($large_number); // expected: int(2147483647) actual: int (2147483647)
$large_number = 2147483648;
var_dump($large_number); // expected: float(2147483648) actual: int (-2147483648)
Even more strangely, the other example from the docs:
$large_number = 9223372036854775807;
var_dump($large_number); // expected: int(9223372036854775807) actual: int(-1)
$large_number = 9223372036854775808;
var_dump($large_number);
// expected: float(9.2233720368548E+18), actual: float(9.2233720368548E+18)
Is this a bug, or am I misunderstanding something? The only similar bug I've found deals with incorrect output of var_dump(PHP_INT_MAX)
with xdebug (which is present in my version), but that doesn't seem to explain what's happening here. If anyone knows of relevant info I should include from phpinfo
I can add it.
An integer overflow occurs when you attempt to store inside an integer variable a value that is larger than the maximum value the variable can hold. The C standard defines this situation as undefined behavior (meaning that anything might happen).
In languages where integer overflow can occur, you can reduce its likelihood by using larger integer types, like Java's long or C's long long int. If you need to store something even bigger, there are libraries built to handle arbitrarily large numbers.
In computer programming, an integer overflow occurs when an arithmetic operation attempts to create a numeric value that is outside of the range that can be represented with a given number of digits – either higher than the maximum or lower than the minimum representable value.
In contrast, the C standard says that signed integer overflow leads to undefined behavior where a program can do anything, including dumping core or overrunning a buffer. The misbehavior can even precede the overflow. Such an overflow can occur during addition, subtraction, multiplication, division, and left shift.
After a helpful comment from @Terminus, I tried setting xdebug.overload_var_dump=0
in php.ini. With that setting, var_dump
produced the correct output. It occurred to me that I had neglected to try simply echo $large_number;
during my testing, so I turned overload_var_dump
back on and echo
produced the expected result while var_dump
did not.
$large_number = 2147483648;
echo $large_number; // 2147483648
var_dump($large_number); // int -2147483648
$large_number = 9223372036854775807;
echo $large_number; // 9223372036854775807
var_dump($large_number); // int -1
So it seems that the bug report I found earlier actually does explain this. The original description in the bug report says:
var_dump() doesn't show correct info about PHP_INT_MAX and PHP_INT_MIN constants on 64bit Windows
But it appears that this is incomplete; it actually shows incorrect info for large number variables as well as the constants.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With