Home >Backend Development >PHP Tutorial >Issues that need to be paid attention to when using PHP's crc32 function (otherwise it will be a pitfall), crc32 function_PHP tutorial
I wrote a table splitting program a few days ago. The hash algorithm used is crc32. The functions of the sub-table are as follows:
Copy code The code is as follows:
Function _getHash($username)
{
$hash = crc32($username) % 512;
return $hash;
}
function _getTable($username)
{
$hash = self::_getHash($username);
return 'user_' . $hash;
}
First, generate the data on the local 32-bit window machine and insert it into the corresponding table. But when I transferred the program and data to the server (64 for Linux), I found that the data could not be found. After investigation, it was discovered that the crc32 results on the server were different from those locally. After checking the PHP manual again, I found out that the crc32 interface is related to the machine.
Description of php manual:
Copy code The code is as follows:
Because PHP's integer type is signed many crc32 checksums will result in negative integers on 32bit platforms. On 64bit installations all crc32() results will be positive integers though.
The result returned by crc32 will overflow on a 32-bit machine, so the result may be negative. On a 64-bit machine, there is no overflow, so it is always positive.
The CRC algorithm is calculated based on the number of bits in the word length.
The crc32 function will calculate PHP_INT_SIZE and PHP_INT_MAX according to the two constant references in php
Definition of these two constants:
The word size of integers is platform-dependent, although the usual maximum is about two billion (32-bit signed). PHP does not support unsigned integers. The word length of an Integer 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.
Output the next 32 bits PHP_INT_SIZE: 4, PHP_INT_MAX: 2147483647
Output PHP_INT_SIZE: 8, PHP_INT_MAX: 9223372036854775807