PHP crc32() Function
Last Updated :
21 Jun, 2023
The crc32() function helps us to calculate a 32-bit crc or cyclic redundancy checksum polynomial for a string. The function uses the CRC32 algorithm.This function can be used to validate data integrity.
However, to ensure that we get the correct string representation from the crc32() function, we need to use the %u formatter of the printf() or sprintf() function. If the %u formatter is not used, the result may display incorrect and negative numbers.
Syntax:
crc32($string)
Parameter:
-
$string: This parameter specifies the string for which we want to find the crc32 polynomial.
Return Value : The crc32() function returns the crc32 checksum of the given string as an integer.
Examples:
Input : Hello world.
Output : 2335835140
Input : Geeks For Geeks.
Output : 2551101144
Below programs illustrate the crc32() function.
Program 1 : This program helps us to calculate a 32-bit CRC for the string “Hello World”, both with %u and without %u.
<?php
$str1 = crc32( "Hello world." );
echo 'Without %u: ' . $str1 . "\n" ;
echo 'With %u: ' ;
printf( "%u\n" , $str1 );
?>
|
Output:
Without %u: 2335835140
With %u: 2335835140
Program 2 : This program helps us to calculate a 32-bit CRC for the string “GeeksforGeeks.”, both with %u and without %u.
<?php
$str2 = crc32( "GeeksforGeeks." );
echo 'Without %u: ' . $str2 . "\n" ;
echo 'With %u: ' ;
printf( "%u\n" , $str2 );
?>
|
Output:
Without %u: 3055367324
With %u: 3055367324
Program 3 : This program helps us to calculate a 32-bit CRC for the string “Computer Science.”, both with %u and without %u.
<?php
$str3 = crc32( "Computer Science." );
echo 'Without %u: ' . $str3 . "\n" ;
echo 'With %u: ' ;
printf( "%u\n" , $str3 );
?>
|
Output:
Without %u: 3212073516
With %u: 3212073516
Reference:
http://php.net/manual/en/function.crc32.php
Share your thoughts in the comments
Please Login to comment...