3

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?

3
  • Could you paste here result of echo PHP_INT_MAX? Commented Aug 19, 2015 at 12:50
  • Looks like they're converted to a float, but they are not too large for a 64-bit integer. Commented Aug 19, 2015 at 12:50
  • I did a print (PHP_INT_MAX) result is 2147483647 Commented Aug 19, 2015 at 12:53

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks so what is the work around this? These numbers represents unique ids in my MySQL db.
Store them as strings, if you do not need to calculate with them, as I suppose.
Thank you for all comments. As I was running Windows via bootcamp, I ended up setting up XAMPP and ran my dev on osx. Shame but osx supports 64bit integers, so happy.
1

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.

1 Comment

Thanks I'm running 64bit windows and 64bit WAMP server, should this be happening?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.