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
$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
- Hash the password.
PASSWORD_DEFAULTpicks bcrypt. - Check the length. A bcrypt hash is always 60 characters.
- Check the start.
$2y$marks a bcrypt hash. - Verify it.
password_verify()confirms the password matches.

