PHP crc32() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, crc32() computes a 32-bit CRC checksum for a string. It is useful for quick integrity checks, not passwords.

You pass one string. It is required. On 32-bit builds the result can look negative—format with %u when you need unsigned hex.

Syntax

Call crc32 on the string. Start with this line.

PHP
$sum = crc32($text);
CallWhat it does
crc32("Hello")Returns an integer checksum.
sprintf("%u", crc32($text))Unsigned decimal form.

CRC32 is not cryptographic. Use password_hash() for passwords and hash_hmac() for secure digests.

Library declarations
PHP
crc32(string $string): int

Parameters

ParameterDescription
stringRequired. The data to checksum.

Return Value

Return ValueDescription
intThe CRC32 checksum value.

PHP Examples

Example 1 — Basic CRC32 Checksum

Pass a string and print the integer result (64-bit PHP shows a positive value).

PHP
<?php
$data = "Hello, PHP!";
$crc  = crc32($data);

echo "CRC32 of '$data': $crc\n";
?>

How It Works

  1. Feed the string. Every byte contributes to the checksum.
  2. Get an int. crc32() returns a 32-bit integer value.
  3. Deterministic. The same string always yields the same CRC on the same platform width rules.
  4. Not secret. Anyone can recompute CRC32—it is not encryption.

Example 2 — Unsigned Decimal and Hex

Format for logs and tools. Hex matches hash("crc32b", $data).

PHP
<?php
$data = "Hello, PHP!";
$crc  = crc32($data);

$hexPad  = str_pad(dechex($crc), 8, "0", STR_PAD_LEFT);
$hashCrc = hash("crc32b", $data);

echo "Raw int:        $crc\n";
echo "Unsigned:       " . sprintf("%u", $crc) . "\n";
echo "Hex padded:     $hexPad\n";
echo "hash(crc32b):   $hashCrc\n";
echo "Hex match:      " . ($hexPad === $hashCrc ? "yes\n" : "no\n");
?>

How It Works

  1. Compute once. Store $crc as an integer.
  2. Unsigned print. %u avoids confusing negatives on 32-bit PHP.
  3. Pad hex to 8. Full 32-bit width for comparisons with other tools.
  4. Same polynomial. hash("crc32b") returns that hex directly.

Example 3 — Verify Data Integrity

Store a checksum with the payload; recompute later to detect accidental changes.

PHP
<?php
$payload = "user=42&action=save";
$stored  = crc32($payload);

$received = "user=42&action=save";
$check    = crc32($received);

if ($check === $stored) {
    echo "Integrity OK — data unchanged.\n";
} else {
    echo "Integrity FAIL — data was modified.\n";
}

$tampered = "user=42&action=save!";
echo "Tampered match: " . (crc32($tampered) === $stored ? "yes" : "no") . "\n";
?>

How It Works

  1. Fingerprint the data. Save crc32($payload) with the record.
  2. Recompute on read. Compare with ===.
  3. Detect edits. Adding ! changes the CRC → mismatch.
  4. Know the limit. Good for accidents; not proof against a clever attacker.

Frequently Asked Questions

crc32() calculates a 32-bit CRC (Cyclic Redundancy Check) checksum of a string and returns it as an integer. It is commonly used for quick data integrity checks, not for cryptographic security.
int crc32(string $string). Pass the data to checksum and receive a 32-bit integer. On 64-bit PHP the result is always a positive integer; on 32-bit systems it may appear negative.
PHP integers are signed. On 32-bit platforms, CRC values above 2^31-1 display as negative numbers even though the underlying checksum is valid. Use sprintf('%u', $crc) for the unsigned decimal string, or dechex($crc) for hexadecimal.
No. CRC32 is designed for error detection, not cryptography. It is fast but vulnerable to collisions and intentional manipulation. Use hash('sha256', $data) or password_hash() for security.
Both compute the same CRC32 polynomial. crc32() returns an integer; hash('crc32b', $data) returns an 8-character lowercase hex string equivalent to str_pad(dechex(crc32($data)), 8, '0', STR_PAD_LEFT).

Next: crypt()

Learn one-way hashing for password storage.

crypt() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful