Don't Panic
Don't Panic

Reputation: 41820

Why are my integers overflowing before they should?

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.

Upvotes: 10

Views: 210

Answers (1)

Don't Panic
Don't Panic

Reputation: 41820

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.

Upvotes: 5

Related Questions