PHP | crc32() Function

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program illustrate the 
// crc32() function
  
$str1 = crc32("Hello world.");
  
// print without %u
echo 'Without %u: '.$str1."\n";
  
// print with %u
echo 'With %u: ';
  
printf("%u\n", $str1);
?>

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
$str2 = crc32("GeeksforGeeks.");
  
// print without %u
echo 'Without %u: '.$str2."\n";
  
// print with %u
echo 'With %u: ';
  
printf("%u\n", $str2);
?>

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
$str3 = crc32("Computer Science.");
  
// print without %u
echo 'Without %u: '.$str3."\n";
  
// print with %u
echo 'With %u: ';
  
printf("%u\n", $str3);
?>

chevron_right


Output:

Without %u: 3212073516
With %u: 3212073516

Reference:
http://php.net/manual/en/function.crc32.php



My Personal Notes arrow_drop_up

Budding Web DeveloperKeen learnerAverage CoderDancer&Social Activist

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.