PHP 8 reference · String Functions
PHP md5() Function
Calculate the MD5 hash of a string as 32 hex characters. Fine for checksums and cache keys, never for passwords.
PHP 8 Since 4.0
Syntax
md5(string $string, bool $binary = false): string
Parameters
| Parameter | Description |
|---|---|
string |
The input string. |
binary optional |
true returns the raw 16-byte digest. Default: false |
Return value
The hash as a hex string.
Examples
<?php
echo md5("hello"), "\n";
echo strlen(md5("hello")), "\n";
Output
5d41402abc4b2a76b9719d911017c592 32
<?php
$cacheKey = "user-list-" . md5(json_encode(["page" => 2, "sort" => "name"]));
echo $cacheKey, "\n";
Output
user-list-3320cd749d441ea9581e5d59a7a70438
Common mistakes
- Never store passwords with
md5()orsha1(). Usepassword_hash()andpassword_verify(). - MD5 is broken for security uses (collisions are easy). Prefer
hash("sha256", …)for integrity checks.
Version history
| Version | Change |
|---|---|
| PHP 4.0 | Added |
Related
sha1()· Calculate the SHA-1 hash of a string as 40 hex characters. Weak for security; prefer hash("sha256", ...).hash()· Hash a string with the algorithm you choose, such as sha256, sha512, crc32b or xxh3. Returns a hex string.base64_encode()· Encode binary data or text as Base64, using only letters, digits, + and /, so it travels safely as text.- PHP Strings · tutorial