Home > Article > Backend Development > PHP built-in function 32-bit and 64-bit platform compatibility issues_PHP tutorial
Bangkejia (www.Bkjia.com) tutorial The integer in PHP is the long type in C language, which is signed, and the maximum value is 2^31. On 64-bit platforms, long can reach 2^63.
In this case, the output results of some PHP functions will be inconsistent on various platforms.
php -r "echo ip2long('255.255.255.255');" On a 64-bit platform it is: 4294967295, on a 32-bit platform it is -1. For example, when filesize is larger than 2G, the results will be inconsistent on different platforms.
Solving this problem is very simple, sprintf("%u", filesize($file)). Convert the result to a string. Why are the results the same: the two's complement of the unsigned number 4294967295 is the same as the two's complement of the signed number -1. Similarly, functions whose return value is int and whose final result may be greater than 2^31 must be handled in this way.
Note that although the returned string is a string, PHP will automatically convert it when performing four arithmetic operations. If the number is greater than 2^31, it will be converted to int. If it is greater than, it will be converted to double.