PHP password_get_info() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Password

Definition

In PHP, password_get_info() reads a password hash and tells you which algorithm made it and which options it used.

It is handy for checking the bcrypt cost or spotting old hashes that are not from password_hash().

Syntax

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

PHP
$info = password_get_info($hash);
CallWhat it does
password_get_info($hash)Returns an array with algo, algoName and options.
password_get_info($hash)['options']['cost']Reads the bcrypt cost.

It only reads the hash format. It does not check any password and never shows the salt.

Library declarations
PHP
password_get_info(string $hash): array

Parameters

ParameterDescription
hashRequired. A hash created by password_hash().

Return Value

Return ValueDescription
arrayKeys: algo (ID string or null), algoName (such as "bcrypt" or "unknown"), and options (such as cost).

PHP Examples

Example 1 — Read the Algorithm and Cost

Inspect a stored bcrypt hash.

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

echo "Algo: ", $info['algo'], PHP_EOL;
echo "Name: ", $info['algoName'], PHP_EOL;
echo "Cost: ", $info['options']['cost'], PHP_EOL;

How It Works

  1. Start with a stored hash. This one is a bcrypt hash.
  2. Call the function. It returns an array.
  3. Read algo and algoName. 2y is the ID, bcrypt is the name.
  4. Read the cost. It lives inside the options array.

Example 2 — A String That Is Not a Hash

Unknown input does not cause an error.

PHP
<?php
$info = password_get_info('not-a-hash');

echo "Algo: ", $info['algo'] ?? 'none', PHP_EOL;
echo "Name: ", $info['algoName'], PHP_EOL;
echo "Options: ", count($info['options']), PHP_EOL;

How It Works

  1. Pass any string. It is not in password_hash() format.
  2. Get a safe result. algo is null and algoName is unknown.
  3. Use ?? for display. It replaces null with the word none.
  4. Count the options. The array is empty.

Example 3 — Spot Legacy Hashes

Tell old md5 hashes apart from bcrypt hashes before you migrate users.

PHP
<?php
$legacy = '5d41402abc4b2a76b9719d911017c592';
$modern = '$2y$07$BCryptRequires22Chrcte/VlQH0piJtjXl.0t1XkA8pw9dMXTpOq';

foreach (['Legacy' => $legacy, 'Modern' => $modern] as $label => $hash) {
    echo $label, " hash: ", password_get_info($hash)['algoName'], PHP_EOL;
}

How It Works

  1. Prepare two hashes. One is a plain md5 string, one is bcrypt.
  2. Loop over them. Each is passed to the function.
  3. Read algoName. The md5 string is unknown.
  4. Plan a migration. Rehash unknown ones when users log in.

Frequently Asked Questions

It reads a hash made by password_hash() and returns the algorithm ID, the algorithm name, and the options such as the bcrypt cost.
password_get_info(string $hash): array. Pass the stored hash.
algo is null, algoName is "unknown", and options is an empty array.
No. A hash is one-way. The function only reads the format part, such as the algorithm and cost.
Use it to log or check the cost of stored hashes. To decide whether a hash needs upgrading, use password_needs_rehash() instead.

Next: password_hash()

password_hash() creates a safe, salted hash of a password.

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