This article mainly introduces the issues that need to be paid attention to when using php crc32 functions (otherwise, it is a pitfall). If you need a friend, refer
This article mainly introduces the issues that need to be paid attention to when using php crc32 functions (otherwise, it is a pitfall). If you need a friend, refer
I wrote a table sharding program a few days ago. The hash algorithm used is crc32. the table sharding function is as follows:
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 data on the local 32-bit window machine and insert the corresponding table. However, when the program and data are uploaded to the server (64 for linux), no data is found. After troubleshooting, we found that the crc32 results on the original server are different from those on the local server. Check the php manual again to know that the crc32 interface was originally related to the machine.
Php manual description:
The Code is as follows:
Because PHP's integer type is signed into crc32 checksums will result in negative integers on 32bit platforms. On 64bit installations all crc32 () results will be positive integers though.
The results returned by crc32 may overflow on 32-bit machines, so the results may be negative. On a 64-bit host, it does not overflow, so it is always positive.
The CRC algorithm is calculated based on the number of characters in the long bits.
The crc32 function calculates PHP_INT_SIZE and PHP_INT_MAX according to the two constants in php.
Definitions of these two constants:
The length of the integer is related to the platform, although the maximum value is usually about 2 billion (32-Bit Signed ). PHP does not support unsigned integers. The length of the Integer value can be expressed by the constant PHP_INT_SIZE. After PHP 4.4.0 and PHP 5.0.5, the maximum value can be expressed by the constant PHP_INT_MAX.
Output the following 32-bit PHP_INT_SIZE: 4, PHP_INT_MAX: 2147483647
Output 64-bit PHP_INT_SIZE: 8, PHP_INT_MAX: 9223372036854775807