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

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

md5(string $string, bool $binary = false): string

Parameters

ParameterDescription
string The input string.
binary optional true returns the raw 16-byte digest. Default: false

Return value

The hash as a hex string.

Examples

Hash a stringTry it yourself
<?php
echo md5("hello"), "\n";
echo strlen(md5("hello")), "\n";

Output

5d41402abc4b2a76b9719d911017c592
32
A cache keyTry it yourself
<?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() or sha1(). Use password_hash() and password_verify().
  • MD5 is broken for security uses (collisions are easy). Prefer hash("sha256", …) for integrity checks.

Version history

VersionChange
PHP 4.0Added

Was this page helpful?