Computes a 32-bit CRC checksum for detecting accidental changes.
What Is crc32() in PHP?
Computes a 32-bit CRC checksum for detecting accidental changes.
Its syntax is:
crc32(string $string): int
Parameters:
$string— Accepted as string; required argument.
Return type: int. CRC32 is not cryptographically secure. Use hash() or hash_hmac() where integrity must resist tampering.
Availability: Core PHP.
Example 1: Basic Usage
<?php
echo sprintf("%u", crc32("hello"));
Output:
907060870
CRC32 is not cryptographically secure. Use hash() or hash_hmac() where integrity must resist tampering.
Example 2: A Different Input
<?php
echo sprintf("%08x", crc32("data"));
Output:
adf3f363
CRC32 is not cryptographically secure. Use hash() or hash_hmac() where integrity must resist tampering.
Practical Example: Applying It in a Program
<?php
$payload = "order:42"; $checksum = sprintf("%08x", crc32($payload)); echo "Checksum: " . $checksum;
Output:
Checksum: 4e9eb32b
CRC32 is not cryptographically secure. Use hash() or hash_hmac() where integrity must resist tampering.
Important Behavior and Edge Cases
- Key behavior: CRC32 is not cryptographically secure. Use hash() or hash_hmac() where integrity must resist tampering.
- Environment: Core PHP.
Comparison With Related Functions
| Function | Purpose |
|---|---|
crc32() | Computes a 32-bit CRC checksum for detecting accidental changes. |
hash() | hash() supports named cryptographic digest algorithms; crc32() is a checksum, not a cryptographic hash. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Ignoring this behavior: CRC32 is not cryptographically secure | CRC32 is not cryptographically secure. Use hash() or hash_hmac() where integrity must resist tampering. |
Key Takeaways
- Computes a 32-bit CRC checksum for detecting accidental changes.
- CRC32 is not cryptographically secure. Use hash() or hash_hmac() where integrity must resist tampering.
- Core PHP.
Summary: CRC32 is not cryptographically secure. Use hash() or hash_hmac() where integrity must resist tampering.