String Functions
Advertisements

PHP crc32() Function

Topic: PHP String ReferencePrev|Next

Description

The crc32() function calculates the 32-bit CRC (cyclic redundancy checksum) for a string.

This function is usually used to validate the integrity of data being transmitted.

The following table summarizes the technical details of this function.

Return Value: Returns the crc32 checksum of string as an integer.
Version: PHP 4.0.1+

Syntax

The basic syntax of the crc32() function is given with:

crc32(string);

The following example shows the crc32() function in action.

<?php
// Calculating checksum
$checksum = crc32("The mountain peaks are covered with snow.");

// Printing formatted string
printf("%u\n", $checksum);
?>

Warning: Since 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. Therefore, you need to use the "%u" formatter of sprintf() or printf() to get the string representation of the unsigned crc32() checksum in decimal format.


Parameters

The crc32() function accepts the following parameters.

Parameter Description
string Required. Specifies the string to work on.

More Examples

Here're some more examples showing how crc32() function actually works:

In the following example printing checksum with and without the "%u" formatter will be equal.

<?php
// Calculating checksum
$checksum = crc32("What happened to John?");

// Printing checksum normally
print $checksum."<br>";

// Printing checksum with "%u" formatter
printf("%u", $checksum);
?>
Advertisements
Bootstrap UI Design Templates