PHP password_verify() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Password

Definition

In PHP, password_verify() checks whether a plain password matches a hash made by password_hash().

It reads the salt and settings from the hash itself, so you only pass the password and the stored hash.

Syntax

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

PHP
$ok = password_verify($password, $hash);
CallWhat it does
password_verify('secret', $hash)Returns true if the password matches.
password_verify('wrong', $hash)Returns false.

The comparison is timing-safe. It also works with hashes made by crypt().

Library declarations
PHP
password_verify(string $password, string $hash): bool

Parameters

ParameterDescription
passwordRequired. The plain-text password the user typed.
hashRequired. The hash stored in your database.

Return Value

Return ValueDescription
booltrue if the password matches the hash, otherwise false.

PHP Examples

Example 1 — Check a Password

Test a right and a wrong password against a stored hash.

PHP
<?php
$hash = '$2y$07$BCryptRequires22Chrcte/VlQH0piJtjXl.0t1XkA8pw9dMXTpOq';

echo password_verify('rasmuslerdorf', $hash) ? "Password is valid" : "Invalid password", PHP_EOL;
echo password_verify('wrong-pass', $hash) ? "Password is valid" : "Invalid password", PHP_EOL;

How It Works

  1. Load the stored hash. It came from password_hash().
  2. Pass the typed password. The function reads the salt from the hash.
  3. Get true for a match. The first call matches.
  4. Get false for a mismatch. The second call does not.

Example 2 — Passwords Are Case-Sensitive

A different letter case is a different password.

PHP
<?php
$hash = '$2y$07$BCryptRequires22Chrcte/VlQH0piJtjXl.0t1XkA8pw9dMXTpOq';

echo password_verify('RasmusLerdorf', $hash) ? "match" : "no match", PHP_EOL;

How It Works

  1. Change one letter. R is now uppercase.
  2. Pass it to the function. The check runs on exact bytes.
  3. Get false. The password does not match.
  4. Do not trim or change case. Verify exactly what the user typed.

Example 3 — A Simple Login Check

Look up a user, then verify the password.

PHP
<?php
$users = ['asha' => '$2y$07$BCryptRequires22Chrcte/VlQH0piJtjXl.0t1XkA8pw9dMXTpOq'];

function checkLogin(array $users, string $name, string $pass): string
{
    if (!isset($users[$name])) {
        return "Unknown user";
    }
    return password_verify($pass, $users[$name]) ? "Welcome, $name" : "Wrong password";
}

echo checkLogin($users, 'asha', 'rasmuslerdorf'), PHP_EOL;
echo checkLogin($users, 'asha', 'oops'), PHP_EOL;
echo checkLogin($users, 'bob', 'rasmuslerdorf'), PHP_EOL;

How It Works

  1. Find the user. A missing name stops early.
  2. Verify the password. The stored hash is the second argument.
  3. Return a message. True welcomes the user.
  4. Use one message in real apps. "Invalid login" avoids revealing which names exist.

Frequently Asked Questions

It checks a plain password against a hash created by password_hash() and returns true when they match.
password_verify(string $password, string $hash): bool. The first argument is the typed password, the second is the stored hash.
Each hash has its own random salt, so hashing the same password twice gives different strings. password_verify() reads the salt from the stored hash and compares correctly.
Yes. "Secret" and "secret" are different passwords.
Yes. It compares in constant time, so response speed does not leak how much of the password was right.

Back to PHP Password Functions

Browse every password function tutorial.

Password functions →

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