CleverCode recently encountered a PHP project plastic conversion problem. mysqlThere is a field id that is bigint and contains a long integer, such as id = 5147486396. However, the php code is deployed on multiple machines due to historical reasons, among which machine A is a 32-bit system and machine B is a 64-bit system. The problem now is that the page access is normal in the 64-bit system. Access error in 32-bit system. The reason is php shaping overflow.
# getconf LONG_BIT
<?php $id = 5147486396; echo '$id:'.$id."\r\n"; $value = (int)$id; echo '(int)$id:'.$value."\r\n"; $value = intval($id); echo 'intval($id):'.$value."\r\n"; $value = filter_var($id, FILTER_VALIDATE_INT); echo 'filter_var($id, FILTER_VALIDATE_INT):'."\r\n"; var_dump($value); ?>
(int)5147486396的结果是852519100,intval(5147486396)的结果是852519100,filter_var(5147486396, FILTER_VALIDATE_INT)结果是false。
# getconf LONG_BIT
<?php $id = 5147486396; echo '$id:'.$id."\r\n"; $value = (int)$id; echo '(int)$id:'.$value."\r\n"; $value = intval($id); echo 'intval($id):'.$value."\r\n"; $value = filter_var($id, FILTER_VALIDATE_INT); echo 'filter_var($id, FILTER_VALIDATE_INT):'."\r\n"; var_dump($value); ?>
(int)5147486396的结果是5147486396,intval(5147486396)的结果是5147486396,filter_var(5147486396, FILTER_VALIDATE_INT)结果是5147486396。
The word length of integers is platform-dependent, although the usual maximum is about two billion (32-bit signed). The maximum value on 64-bit platforms is usually around 9E18. PHP does not support unsigned integers. Integer The word length of the value can be represented by the constant PHP_INT_SIZE. Since PHP 4.4.0 and PHP 5.0.5, the maximum value can be represented by the constant PHP_INT_MAX
The above is the detailed content of Detailed introduction to long integer forced conversion overflow in PHP 32-bit system. For more information, please follow other related articles on the PHP Chinese website!