PHP Long integer forced conversion overflow in 32-bit system

Source: Internet
Author: User

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 several machines for historical reasons, including a machine 32-bit system, B machine 64 system. The problem now is that page access is normal in the 64 system. There was an error accessing 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);? >

1.3 Result of the run result (int) 5147486396 is the result of 852519100,intval (5147486396) is 852519100,filter_var (5147486396, Filter_validate_int) The 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 running result (int) 5147486396 is that the result of 5147486396,intval (5147486396) is 5147486396,filter_var (5147486396, Filter_validate_ INT) The result is 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 a 64-bit platform is usually approximately 9E18. PHP does not support unsigned integers. The length of the 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 expressed as 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 Programmer technology Essence): http://blog.csdn.net/clevercode/article/category/3169943 (blog continues to increase, concern please collection).
4) Welcome attention to Clevercode Blog more 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.