PHP password_hash() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Password

Definition

In PHP, password_hash() turns a plain password into a safe hash that you can store in a database.

It adds a random salt for you and uses a strong algorithm, so you do not need to manage either.

Syntax

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

PHP
$hash = password_hash($password, PASSWORD_DEFAULT);
CallWhat it does
password_hash('secret', PASSWORD_DEFAULT)Hashes with the default algorithm (bcrypt today).
password_hash('secret', PASSWORD_BCRYPT, ['cost' => 12])Uses bcrypt with a chosen cost.
password_hash('secret', PASSWORD_ARGON2ID)Uses Argon2id when PHP supports it.

The result is different on every call because a new random salt is used. Check passwords with password_verify(), never with ==.

Library declarations
PHP
password_hash(string $password, string|int|null $algo, array $options = []): string

Parameters

ParameterDescription
passwordRequired. The plain-text password. bcrypt only uses the first 72 bytes.
algoRequired. PASSWORD_DEFAULT, PASSWORD_BCRYPT, PASSWORD_ARGON2I or PASSWORD_ARGON2ID.
optionsOptional. Settings such as cost (bcrypt) or memory_cost, time_cost and threads (Argon2). The salt option was removed in PHP 8.0.

Return Value

Return ValueDescription
stringThe hash, which includes the algorithm, cost and salt. A bcrypt hash is 60 characters long.

PHP Examples

Example 1 — Hash and Verify a Password

Create a hash with the default algorithm and check it. The hash itself is random, so the example prints its length and start.

PHP
<?php
$hash = password_hash("S3cret!pass", PASSWORD_DEFAULT);

echo "Length: ", strlen($hash), PHP_EOL;
echo "Starts with: ", substr($hash, 0, 4), PHP_EOL;
echo "Matches: ", password_verify("S3cret!pass", $hash) ? "yes" : "no", PHP_EOL;

How It Works

  1. Hash the password. PASSWORD_DEFAULT picks bcrypt.
  2. Check the length. A bcrypt hash is always 60 characters.
  3. Check the start. $2y$ marks a bcrypt hash.
  4. Verify it. password_verify() confirms the password matches.

Example 2 — The Same Password Gives Different Hashes

Each call adds a new salt.

PHP
<?php
$a = password_hash("same", PASSWORD_DEFAULT);
$b = password_hash("same", PASSWORD_DEFAULT);

echo "Hashes equal: ", $a === $b ? "yes" : "no", PHP_EOL;
echo "Both verify: ", (password_verify("same", $a) && password_verify("same", $b)) ? "yes" : "no", PHP_EOL;

How It Works

  1. Hash twice. The password is the same both times.
  2. Compare the hashes. They differ because the salts differ.
  3. Verify both. Each hash stores its own salt.
  4. Never use ===. Always check with password_verify().

Example 3 — Set the bcrypt Cost

A higher cost makes hashing slower and harder to attack.

PHP
<?php
$hash = password_hash("S3cret!pass", PASSWORD_BCRYPT, ["cost" => 12]);
$info = password_get_info($hash);

echo "Algorithm: ", $info['algoName'], PHP_EOL;
echo "Cost: ", $info['options']['cost'], PHP_EOL;

How It Works

  1. Pass options. cost is a number from 4 to 31.
  2. Hash the password. Each cost step doubles the work.
  3. Read the result. password_get_info() shows what was used.
  4. Tune for your server. Aim for about 50 to 250 milliseconds per hash.

Frequently Asked Questions

It creates a one-way hash of a password with a random salt, using a strong algorithm. You store the hash and later check logins with password_verify().
password_hash(string $password, string|int|null $algo, array $options = []): string. Most code uses PASSWORD_DEFAULT as the algorithm.
A new random salt is added on each call. That is expected. password_verify() reads the salt from the stored hash, so every hash of the same password still verifies.
PASSWORD_DEFAULT. It is bcrypt today and PHP can move it to a stronger algorithm later. Argon2id is also a good choice when PHP supports it.
No. The salt option was removed in PHP 8.0 and was already ignored in PHP 7. The function creates a secure salt for you.

Next: password_needs_rehash()

password_needs_rehash() tells you when a stored hash should be upgraded.

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