PHP 8.5 crc32() Function With Example

The crc32() function remains available in PHP 8.5 and is used to generate a 32-bit cyclic redundancy checksum for a string, typically for validating data integrity . However, because PHP is integers are signed, the function may return a negative number on 32-bit systems, so it is recommended to format the result as an unsigned integer using printf() or sprintf() .

Code Example

<?php
$data = "Hello World";

// 1. Get the raw checksum
$checksum = crc32($data);

// 2. Output the raw result (May be negative on 32-bit systems)
echo "Direct output: " . $checksum . " ";

// 3. Output as an unsigned integer (Best practice for consistency)
printf("Unsigned output: %u ", $checksum);

// 4. Output as a hexadecimal string (Useful for short URLs/IDs)
printf("Hex output: %x ", $checksum);

// 5. Alternative way to get the hex output using dechex()
echo "Hex output (using dechex): " . dechex($checksum) . " ";
?>

The Output

If you run this code on a 64-bit system (which is standard nowadays), the exact output will be:

Direct output: 4151316296
Unsigned output: 4151316296
Hex output: f76e6968
Hex output (using dechex): f76e6968

 

6 Views