Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Web and Security

PHP password_hash() function

Reference for PHP password_hash(): hash passwords with bcrypt or Argon2id, choose cost options, understand the salt and output format, and avoid common mistakes.

password_hash() turns a plain-text password into a string that is safe to store in a database. It picks a slow, salted algorithm, generates a fresh random salt for every call, and returns one self-describing string that contains the algorithm, its settings, the salt and the hash.

You never store the password itself, and you never compute the hash with md5() or hash(): those are fast on purpose, which helps attackers. Check a login attempt later with password_verify().

PHP
$hash = password_hash($password, PASSWORD_DEFAULT);

Try it

PHP Hash and verify

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP Algorithms and options

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
password_hash(
    string $password,
    string|int|null $algo,
    array $options = []
): string
Name Description
$password The plain-text password.
$algo PASSWORD_DEFAULT, PASSWORD_BCRYPT, PASSWORD_ARGON2I or PASSWORD_ARGON2ID (the Argon2 constants exist only when PHP was built with them). null means the default.
$options Algorithm settings. Bcrypt: cost (default 12 since PHP 8.4). Argon2: memory_cost, time_cost, threads.

Return value: the hash as a string. Bcrypt hashes are 60 characters and start with $2y$; Argon2 hashes are longer and start with $argon2id$.

Errors: a ValueError for an unknown algorithm or invalid options. Passwords containing a null byte also throw a ValueError for bcrypt.

Usage notes

  • Use PASSWORD_DEFAULT unless you have a reason not to. It is bcrypt today and may change in a future release, which is why the database column should be able to hold at least 255 characters.
  • The salt is automatic. Do not pass your own: the salt option was removed in PHP 8.0.
  • Never compare hashes with ===. Hashing the same password twice gives different strings. Always use password_verify().
  • Bcrypt only reads the first 72 bytes of the password. Very long passwords are cut, and a null byte is rejected. Do not pre-hash the password with md5() to work around this.
  • Raise the cost over time. Pick the highest cost your server can afford, around 50 ms per hash, and upgrade old hashes at login with password_needs_rehash().
  • Argon2id is memory-hard and a good choice when available. The CLI used in the examples here has it, but shared hosting may not.
  • Output changes every run. That is why the examples print only prefixes and lengths.
  • Not for tokens or API keys. Use random_bytes() for secrets and hash() for fast fingerprints.