PHP - cast from large float to int will return 0
我无法解释以下行为。当使用(int)转换非常大的整数(由PHP处理为浮点数)时,我得到以下结果:
1 2 3 4 5 6 7 8 9 10 11 | <?php print (int)9223372036854775800; // returns: 9223372036854775800 echo"<br />"; print (int)11702667999999911110; // returns: -6744076073709639680 echo"<br />"; print (int)17999997999999911110; // returns: -446746073709639680 echo"<br />"; print (int)18400997999999911110; // returns: -45746073709639680 echo"<br />"; print (int)41702667999999999990; // returns: 0 ?> |
为什么PHP返回这些值?
您超出了最大值,因此它正在执行PHP.net网站上的奇怪操作。您必须检查自己的最大值,但看起来大于9E18。
The size of an integer is platform-dependent, although a maximum value of about two billion is the usual value (that's 32 bits signed). 64-bit platforms usually have a maximum value of about 9E18. PHP does not support unsigned integers. Integer size can be determined using the constant PHP_INT_SIZE, and maximum value using the constant PHP_INT_MAX since PHP 4.4.0 and PHP 5.0.5.
您超出了PHP_INT_MAX的值,该值是PHP可以支持的最大整数,在32位系统上通常为2,147,483,647。较大的数字将切换为浮点类型。