PHP crc32()Functions

PHP String 参考手册PHP String Reference Manual

Examples

Output the result of crc32():

<?php $str = crc32("Hello World!"); printf("%un",$str); ?>

Run Example »

Definition and Usage

The crc32() function calculates a 32-bit CRC (cyclic redundancy check) of a string.

This function can be used to verify data integrity.

Tip:To ensure that you get the correct string representation from the crc32() function, you must use the %u format specifier of the printf() or sprintf() functions. If the %u format specifier is not used, the result may be displayed as an incorrect number or a negative number.


Syntax

crc32(string)

Parameter Description
string Required. Specifies the string to be calculated.

Technical Details

Return value: Returns, as an integer,stringthe 32-bit cyclic redundancy check polynomial of the string.
PHP version: 4.0.1+


Example 1

In this example, we will output the result of crc32() with and without using the "%u" format specifier (note that the results are the same):

Example

<?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 using the "%u" format specifier (note that the results are not the same):

Example

<?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


PHP String 参考手册PHP String Reference Manual

Other Extensions