Json_decode integer overflow solution _ PHP Tutorial

Source: Internet
Author: User
Json_decode integer overflow solution. This is a bit like if the mysqlint type is exceeded, and we only need to use bigint, so this type of problem may also occur due to the int data range in php. Encoding is a bit like overflow when the mysql int type is exceeded, and we only need to use bigint type. this problem may also occur due to the int data range in php.

An error occurs during encoding, that is, when processing json, the int value with a large value is damaged after decoding, for example:

The code is as follows:

$ Array = array (
"Id1" => 2147483647,
"Id2" = & gt; 2147483648
);
$ Json = json_encode ($ array );
$ Out = json_decode ($ json, true );
Var_dump ($ out );
Theoretically, we can see that:

Array (2 ){
["Id1"] = & gt; int (2147483647)
["Id2"] = & gt; int (2147483648)
}

But on my computer, I actually got:

The code is as follows:
Array (2 ){
["Id1"] = & gt; int (2147483647)
["Id2"] => int (-2147483646)
}

This is determined by the PHP integer range, which depends on the operating system. In a 32-bit operating system, the maximum integer value of PHP is 2147483647. you can see it by outputting PHP_INT_MAX.

In general, if you assign a large value, PHP will automatically determine the range of the value and automatically convert the type, such:

The code is as follows:
$ Large_number = 2147483647;
Var_dump ($ large_number); // int (2147483647)

$ Large_number = 2147483648;
Var_dump ($ large_number); // float (2147483648)

$ Million = 1000000;
$ Large_number = 50000 * $ million;
Var_dump ($ large_number); // float (50000000000)


However, this check is not performed in the json_decode method. this is a bug in PHP (earlier version). in versions later than 5.3, this problem does not exist.

If you do not want to update your PHP, you can convert the number into a string. Take the above code as an example:

The code is as follows:
$ Array = array (
"Id1" => 2147483647,
"Id2" = & gt; 2147483648
);
$ Json = json_encode ($ array );

$ Json = preg_replace ('/("idd" :) (d {9,})/I', '$ {1} "$ {2}"', $ json );

$ Out = json_decode ($ json, true );
Var_dump ($ out );

Of course, how to replace this is on demand and requires careful testing.

When the struct int type is exceeded, it only needs to use the bigint type. This type of problem may also occur due to the int data range in php. Encoding...

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.