PHP password_needs_rehash() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Password

Definition

In PHP, password_needs_rehash() checks whether a stored hash matches the algorithm and options you want today.

Call it after a successful login. If it returns true, hash the password again and save the new hash.

Syntax

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

PHP
$needs = password_needs_rehash($hash, PASSWORD_DEFAULT, $options);
CallWhat it does
password_needs_rehash($hash, PASSWORD_DEFAULT)True if the hash is not the default algorithm or cost.
password_needs_rehash($hash, PASSWORD_BCRYPT, ['cost' => 12])True if the hash cost is not 12.

Pass the same algorithm and options you use with password_hash(). It only compares settings, not the password.

Library declarations
PHP
password_needs_rehash(string $hash, string|int|null $algo, array $options = []): bool

Parameters

ParameterDescription
hashRequired. The stored hash.
algoRequired. The algorithm you want, such as PASSWORD_DEFAULT.
optionsOptional. The options you want, such as cost.

Return Value

Return ValueDescription
booltrue if the hash does not match the algorithm and options, false if it is already up to date.

PHP Examples

Example 1 — Cost Is Too Low

The stored hash uses cost 7, but we want cost 10.

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

if (password_needs_rehash($hash, PASSWORD_BCRYPT, ['cost' => 10])) {
    echo "Rehash needed", PHP_EOL;
} else {
    echo "Hash is current", PHP_EOL;
}

How It Works

  1. Start with a stored hash. Its cost is 7.
  2. State what you want. bcrypt with cost 10.
  3. Compare the settings. They are different.
  4. Get true. The hash should be upgraded.

Example 2 — Hash Already Matches

Ask for the same settings the hash already uses.

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

echo password_needs_rehash($hash, PASSWORD_BCRYPT, ['cost' => 7]) ? "Rehash needed" : "Hash is current", PHP_EOL;

How It Works

  1. Reuse the same hash. It is bcrypt with cost 7.
  2. Ask for bcrypt with cost 7. The settings are the same.
  3. Get false. No upgrade is needed.
  4. Skip the extra work. Hashing again would waste time.

Example 3 — Upgrade the Hash at Login

Verify first, then rehash when the settings are old.

PHP
<?php
function login(string $password, string $hash): string
{
    if (!password_verify($password, $hash)) {
        return "Wrong password";
    }

    $options = ['cost' => 10];
    if (password_needs_rehash($hash, PASSWORD_BCRYPT, $options)) {
        $new = password_hash($password, PASSWORD_BCRYPT, $options);
        $cost = password_get_info($new)['options']['cost'];
        return "Login ok, hash upgraded to cost $cost";
    }

    return "Login ok";
}

$hash = '$2y$07$BCryptRequires22Chrcte/VlQH0piJtjXl.0t1XkA8pw9dMXTpOq';

echo login("rasmuslerdorf", $hash), PHP_EOL;
echo login("wrong", $hash), PHP_EOL;

How It Works

  1. Verify the password. A wrong password stops here.
  2. Check the settings. The old hash has cost 7.
  3. Rehash. password_hash() makes a new cost 10 hash.
  4. Save it. In a real app you would update the database row.

Frequently Asked Questions

It checks if a stored hash was made with different settings than the ones you want now, such as a lower bcrypt cost or an older algorithm.
password_needs_rehash(string $hash, string|int|null $algo, array $options = []): bool. Use the same algo and options as password_hash().
Right after password_verify() succeeds. That is the only moment you have the plain password and can create a new hash.
PASSWORD_DEFAULT can change. PHP 8.4 raised the default bcrypt cost from 10 to 12, so older hashes report true. Set an explicit cost if you want control.
No. It only compares the hash settings. Use password_verify() to check the password.

Next: password_verify()

password_verify() checks a password against a stored hash.

password_verify() 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