Generates the cyclic redundancy checksum polynomial of 32-bit lengths of the str. This is usually used to validate the integrity of data being transmitted. Test crc32 online.
int crc32 ( string $str )
PHP Documentation by the PHP Documentation Group
(PHP 4 >= 4.0.1, PHP 5, PHP 7)
crc32 — Calculates the crc32 polynomial of a string
$str) : int
Generates the cyclic redundancy checksum polynomial of 32-bit
lengths of the
str. This is usually used
to validate the integrity of data being transmitted.
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.
For a hexadecimal representation of the checksum you can either use the "%x" formatter of sprintf() or printf() or the dechex() conversion functions, both of these also take care of converting the crc32() result to an unsigned integer.
Having 64bit installations also return negative integers for higher result values was considered but would break the hexadecimal conversion as negatives would get an extra 0xFFFFFFFF######## offset then. As hexadecimal representation seems to be the most common use case we decided to not break this even if it breaks direct decimal comparisons in about 50% of the cases when moving from 32 to 64bits.
In retrospect having the function return an integer maybe wasn't the best idea and returning a hex string representation right away (as e.g. md5() does) might have been a better plan to begin with.
For a more portable solution you may also consider the generic
hash("crc32b", $str) will
return the same string as
str_pad(dechex(crc32($str)), 8, '0', STR_PAD_LEFT).
Returns the crc32 checksum of
str as an integer.
Example #1 Displaying a crc32 checksum
This example shows how to print a converted checksum with the printf() function:
$checksum = crc32("The quick brown fox jumped over the lazy dog.");
Copyright © 1997 - 2016 by the PHP Documentation Group. This material may be distributed only subject to the terms and conditions set forth in the Creative Commons Attribution 3.0 License or later. A copy of the Creative Commons Attribution 3.0 license is distributed with this manual. The latest version is presently available at » http://creativecommons.org/licenses/by/3.0/.