PHP hash_equals() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Hash

Definition

In PHP, hash_equals() compares two strings and takes the same time whether they match or not.

Use it to check tokens, signatures and hashes, where a normal comparison could leak how many characters were right.

Syntax

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

PHP
$same = hash_equals($known, $user);
CallWhat it does
hash_equals('abc', 'abc')Returns true.
hash_equals('abc', 'abd')Returns false.

Both arguments must be strings. In PHP 8 an int or null throws a TypeError.

Library declarations
PHP
hash_equals(string $known_string, string $user_string): bool

Parameters

ParameterDescription
known_stringRequired. The trusted string, such as the hash you computed.
user_stringRequired. The string from the user, such as a token in the request.

Return Value

Return ValueDescription
booltrue if the two strings are exactly equal, otherwise false.

PHP Examples

Example 1 — Compare Two Tokens

A match returns true.

PHP
<?php
$saved = 'abc123token';

echo hash_equals($saved, 'abc123token') ? "Tokens match" : "Tokens differ", PHP_EOL;
echo hash_equals($saved, 'abc123tokeN') ? "Tokens match" : "Tokens differ", PHP_EOL;

How It Works

  1. Keep the trusted token first. It comes from your server.
  2. Pass the user token second. It comes from the request.
  3. Get true for equal strings. The first call matches.
  4. Get false for any difference. Even one letter counts.

Example 2 — Both Values Must Be Strings

An int throws a TypeError in PHP 8.

PHP
<?php
try {
    hash_equals(12345, '12345');
} catch (TypeError $e) {
    echo $e->getMessage(), PHP_EOL;
}

How It Works

  1. Pass an int by mistake. The function wants strings.
  2. PHP throws a TypeError. It names the bad argument.
  3. Catch it. Print the message to see the cause.
  4. Fix it. Use (string) $value before you compare.

Example 3 — Check an HMAC Signature

Sign a message, then verify what was received.

PHP
<?php
$secret  = 'demo-secret';
$payload = 'order=1042';
$sent    = hash_hmac('sha256', $payload, $secret);

foreach (['order=1042', 'order=9999'] as $received) {
    $expected = hash_hmac('sha256', $received, $secret);
    echo $received, ": ", hash_equals($sent, $expected) ? "valid" : "tampered", PHP_EOL;
}

How It Works

  1. Sign the payload. hash_hmac() uses the secret key.
  2. Recompute for each message. Use the same key.
  3. Compare with hash_equals(). The check is timing-safe.
  4. Reject changes. A different payload gives a different signature.

Frequently Asked Questions

It compares two strings in constant time and returns true if they are identical. The time taken does not depend on where they differ.
hash_equals(string $known_string, string $user_string): bool. The first argument is the trusted value.
A normal comparison stops at the first different character. An attacker can measure tiny time differences and guess a token one character at a time.
For password_hash() hashes use password_verify(). Use hash_equals() for tokens, HMAC signatures and other hash strings.
In PHP 8 it throws a TypeError. In PHP 7 it raised a warning and returned false. Cast with (string) when needed.

Next: hash_file()

hash_file() hashes the contents of a file.

hash_file() 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.

8 people found this page helpful