Tutorialspoint.dev

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.

<?php
// PHP program illustrate the 
// crc32() function
  
$str1 = crc32("Hello world.");
  
// print without %u
echo 'Without %u: '.$str1." ";
  
// print with %u
echo 'With %u: ';
  
printf("%u ", $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.");
  
// print without %u
echo 'Without %u: '.$str2." ";
  
// print with %u
echo 'With %u: ';
  
printf("%u ", $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.");
  
// print without %u
echo 'Without %u: '.$str3." ";
  
// print with %u
echo 'With %u: ';
  
printf("%u ", $str3);
?>

Output:

Without %u: 3212073516
With %u: 3212073516

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



This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter