Previously I have been developing an app using PHP 5.6.7 in Linux, however recently I've switched to a Windows environment using WAMP 64 bit and upgraded PHP to 5.6.12 and been running into a few issues. One issue is in the backend PHP I have an array of integers. When I print to return to the front which prints the return array to the console I get a different format. That is:
$permission->fk_org_id = [24053826281537536,24051529749102626,111];
print json_encode($permission->fk_org_id);
Returns the following to the console:
0:24053826281538000
1:24051529749103000
2:111
Why is this happening?
Those numbers are too large to fit in a (32-bit) integer. They will be interpreted as a float, see the documentation:
If PHP encounters a number beyond the bounds of the integer type, it will be interpreted as a float instead. Also, an operation which results in a number beyond the bounds of the integer type will return a float instead.
A float has a precision of about 14 significant digits, so that's why you see the zeroes.
Unfortunately, PHP on Windows does not support 64 bit integers, according to this answer:
On windows x86_64, PHP_INT_MAX is 2147483647. This is because in the underlying c-code, a long is 32 bit.
However, linux on x86_64 uses a 64bit long so PHP_INT_MAX is going to be 9223372036854775807.
You probably run your script on 32-bit machine where max int is 2147483647, so integer overflow occurres. According to documentation:
If PHP encounters a number beyond the bounds of the integer type, it will be interpreted as a float instead. Also, an operation which results in a number beyond the bounds of the integer type will return a float instead.
<?php
$large_number = 2147483647;
var_dump($large_number); // int(2147483647)
$large_number = 2147483648;
var_dump($large_number); // float(2147483648)
$million = 1000000;
$large_number = 50000 * $million;
var_dump($large_number); // float(50000000000)
?>
More about integers you can find in documentation.
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