Home > Backend Development > PHP Tutorial > Detailed introduction to long integer forced conversion overflow in PHP 32-bit system

Detailed introduction to long integer forced conversion overflow in PHP 32-bit system

黄舟
Release: 2023-03-14 09:40:02
Original
2572 people have browsed it

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.

1 A machine demonstration

1.1 Get the system bit number of A machine

# getconf LONG_BIT


1.2 Shaping conversion code

<?php

$id = 5147486396;
echo &#39;$id:&#39;.$id."\r\n";

$value = (int)$id;
echo &#39;(int)$id:&#39;.$value."\r\n";

$value = intval($id);
echo &#39;intval($id):&#39;.$value."\r\n";

$value = filter_var($id, FILTER_VALIDATE_INT);
echo &#39;filter_var($id, FILTER_VALIDATE_INT):&#39;."\r\n";
var_dump($value);


?>
Copy after login
Copy after login

1.3 Running results

(int)5147486396的结果是852519100,intval(5147486396)的结果是852519100,filter_var(5147486396, FILTER_VALIDATE_INT)结果是false。
Copy after login


2 Machine B demonstration

2.1 Get the number of system digits of B machine

# getconf LONG_BIT


2.2 Plastic conversion code

<?php

$id = 5147486396;
echo &#39;$id:&#39;.$id."\r\n";

$value = (int)$id;
echo &#39;(int)$id:&#39;.$value."\r\n";

$value = intval($id);
echo &#39;intval($id):&#39;.$value."\r\n";

$value = filter_var($id, FILTER_VALIDATE_INT);
echo &#39;filter_var($id, FILTER_VALIDATE_INT):&#39;."\r\n";
var_dump($value);


?>
Copy after login
Copy after login

2.3 Running result

(int)5147486396的结果是5147486396,intval(5147486396)的结果是5147486396,filter_var(5147486396, FILTER_VALIDATE_INT)结果是5147486396。
Copy after login


3 Conclusion

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template