PHP md5() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, md5() calculates the MD5 hash of a string.

It returns a 32-character hexadecimal string. The same input always gives the same hash.

Syntax

Call md5() with the values you need. Start with this line.

PHP
$hash = md5("hello");
CallWhat it does
md5("hello")Returns "5d41402abc4b2a76b9719d911017c592".
md5("")Returns "d41d8cd98f00b204e9800998ecf8427e".
md5("hello", true)Returns the raw 16-byte binary hash.

MD5 is fine for quick checksums and cache keys. It is not safe for passwords or security.

Library declarations
PHP
md5(string $string, bool $binary = false): string

Parameters

ParameterDescription
stringRequired. The string to hash.
binaryOptional. If true, returns a 16-byte raw binary string. Default is false, which returns 32 hex characters.

Return Value

Return ValueDescription
stringThe MD5 hash, as 32 hex characters or 16 raw bytes.

PHP Examples

Example 1 — Hash a String

Pass a string in and get a 32-character hash back.

PHP
<?php
$hash = md5("hello");

echo $hash, PHP_EOL;
echo strlen($hash), PHP_EOL;

How It Works

  1. Call md5 with a string. The text here is hello.
  2. Get a hex hash. It looks random but is always the same for this input.
  3. Count the length. strlen shows 32 characters.
  4. Rerun anytime. The same input gives the same hash every time.

Example 2 — Empty String and Binary Output

An empty string still has a hash, and the binary flag changes the length.

PHP
<?php
echo md5(""), PHP_EOL;
echo strlen(md5("hello")), PHP_EOL;
echo strlen(md5("hello", true)), PHP_EOL;

How It Works

  1. Hash nothing. An empty string has a well-known MD5 value.
  2. Default output. Hex text is 32 characters long.
  3. Binary output. The raw form is only 16 bytes.
  4. Use binary carefully. Raw bytes are not safe to print directly.

Example 3 — Hashes Are Case Sensitive

A tiny change in the input gives a very different hash.

PHP
<?php
echo md5("Hello"), PHP_EOL;
echo md5("hello"), PHP_EOL;

How It Works

  1. Hash Hello. It starts with a capital H.
  2. Hash hello. It starts with a lowercase h.
  3. Compare the output. The two hashes look nothing alike.
  4. Use this idea. Hashes help detect even small changes in data.

Frequently Asked Questions

md5() returns the MD5 hash of a string as a 32-character hexadecimal value, or as 16 raw bytes if the second argument is true.
string md5(string $string, bool $binary = false). The binary flag is optional.
No. MD5 is too fast and has known weaknesses. Use password_hash() with password_verify() to store passwords.
No. A hash is one-way. Attackers can guess common inputs, which is why MD5 should not protect secrets.
Use md5_file() with the file path. It reads the file and returns its hash without loading the whole file into a variable.

Next: md5_file()

md5_file() calculates the MD5 hash of a file.

md5_file() tutorial →

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.

8 people found this page helpful