This article mainly introduces the issues that need to be paid attention to when using the crc32 function of php (otherwise it will be a pitfall), friends who need it You can refer to the following
I wrote a table splitting program a few days ago. The hash algorithm used is crc32. The table splitting function is as follows:
Copy the 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.
php manual description:
Copy the 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 a negative number. 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 in 64 bits