Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PHP - crc32() Function



The PHP crc32() function is used to calculate the 32-bit Cyclic Redundancy Check (CRC32) of a given string. The "Cyclic Redundancy Check (CRC)" is an error-detecting code used to confirm the integrity of data during transmission or storage.

This function returns a 32-bit integer representing the CRC32 checksum, which can be used for data integrity checks or other many purposes.

In PHP, many "CRC32" checksums will result in negative integers on 32-bit platforms. To get the unsigned CRC32 checksum in decimal or hexadecimal format, you should use the %u and %x format specifiers with sprintf() or printf().

Syntax

Following is the syntax of the PHP crc32() function −

crc32(string $str): int

Parameters

This function accepts a single parameter, which is listed below −

  • str − The input string for which the CRC32 checksum is to be calculated.

Return Value

This function returns a 32-bit CRC as a string.

Example 1

The following is the basic example of the PHP crc32() function −

<?php
   $str = "Hello from Tutorialspoint";
   echo "The given string: $str";
   echo "\nThe 32-bit crc is: ";
   #using crc32() function
   $crc = crc32($str);
   printf("%u\n",$crc);
?>

Output

After executing the above program, the following output will be displayed −

The given string: Hello from Tutorialspoint
The 32-bit crc is: 1069189824

Example 2

Below is an example of the PHP crc32() function. We use this function to calculate the cyclic redundancy checksum (CRC32) of the given string "Hello World!" and return the result in "hexadecimal" format −

<?php
   $str = "Hello World!";
   echo "The given string: $str";
   echo "\nThe 32-bit crc is (in hexadecimal): ";
   #using crc32() function
   $crc = crc32($str);
   printf("%x\n",$crc);
?>

Output

The above program produces the following output −

The given string: Hello World!
The 32-bit crc is (in hexadecimal): 1c291ca3
php_function_reference.htm
Advertisements