Clevercode recently encountered a PHP project shaping transformation problem, MySQL has a field ID is bigint, there is a long integer, such as id = 5147486396. However, the PHP code is deployed in multiple machines for historical reasons, in a machine 32-bit system, B machine 64 system. The problem today is that page access is normal in the 64 system. An error occurred in the 32-bit system. The reason is that PHP shaping overflows.
1 A machine demo 1.1 get a machine system number of bits
# getconf Long_bit
1.2 Shaping the conversion code
<?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);? >
The result of 1.3 execution result (int) 5147486396 is 852519100. The result of Intval (5147486396) is that 852519100,filter_var (5147486396, Filter_validate_int) result is false.
2 B Machine Demo 2.1 Get B machine system number of bits # getconf Long_bit
2.2 Shaping the conversion code
<?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);? >
The result of 2.3 execution result (int) 5147486396 is 5147486396. The result of Intval (5147486396) is that 5147486396,filter_var (5147486396, Filter_validate_int) results are 5147486396.
3 conclusion The word length of the integer number is related to the platform. Although the usual maximum value is approximately 2 billion (32-bit signed).
The maximum value under the 64-bit platform is generally approximately 9E18.
PHP does not support unsigned integers. The word length of an Integer value can be expressed as a constant php_int_size, since PHP 4.4.0 and PHP 5.0.5. The maximum value can be represented by a constant php_int_max.
Copyright notice: 1) original works, from the "Clevercode blog", strictly prohibited reprint, otherwise hold copyright legal responsibility.
2) Original address: http://blog.csdn.net/clevercode/article/details/46423103.
3) Category address (PHP Program Ape Technology Essence): http://blog.csdn.net/clevercode/article/category/3169943 (blog continues to add. Attention please bookmark).
4) Welcome everyone to pay attention to Clevercode blog many other wonderful content: Http://blog.csdn.net/CleverCode.
5) Welcome attention to Clevercode Weibo: Http://weibo.com/CleverCode.
PHP Long integer forced conversion overflow in 32-bit system