PHP crc32()
Function
Example
Output the result of crc32():
<?php
$str = crc32("Hello World!");
printf("%u\n", $str);
?>
Definition and Usage
The crc32() function calculates the 32-bit CRC (Cyclic Redundancy Check) of a string.
This function can be used to verify the integrity of data.
Note: To ensure you get the correct string representation from the crc32() function, you must use the %u format specifier with the printf() or sprintf() functions. If the %u format specifier is not used, the result may appear as an incorrect number or a negative number.
Syntax
Parameter | Description |
---|---|
string | Required. Specifies the string to be calculated. |
Technical Details
Return Value: | Returns the 32-bit cyclic redundancy checksum polynomial of the string as an integer. |
---|---|
PHP Version: | 4.0.1+ |
--- | --- |
Example 1
In this example, we will output the result of crc32() with and without the "%u" format specifier (note that the results are the same):
<?php
$str = crc32("Hello world!");
echo 'Without %u: ' . $str . "<br>";
echo 'With %u: ';
printf("%u", $str);
?>
The above code will output:
Without %u: 461707669
With %u: 461707669
Example 2
In this example, we will output the result of crc32() with and without the "%u" format specifier (note that the results are different):
<?php
$str = crc32("Hello world.");
echo 'Without %u: ' . $str . "<br>";
echo 'With %u: ';
printf("%u", $str);
?>
The above code will output:
Without %u: -1959132156
With %u: 2335835140