PHP 8 interview question · Most asked
How should passwords be stored in PHP?
Summary
Use password_hash() (bcrypt by default, or Argon2id) and password_verify(), and call password_needs_rehash() on login to upgrade old hashes.
Longer Answer
These functions handle salt generation and embed algorithm and cost in the hash string. Never use md5, sha1 or unsalted hash(). Bcrypt truncates input at 72 bytes; Argon2id (PASSWORD_ARGON2ID) is memory-hard and recommended where available.
Code Example
<?php
$hash = password_hash($password, PASSWORD_ARGON2ID);
if (password_verify($input, $hash)) {
if (password_needs_rehash($hash, PASSWORD_ARGON2ID)) {
$hash = password_hash($input, PASSWORD_ARGON2ID);
// save new hash
}
}
Key Points
password_hash($pw, PASSWORD_DEFAULT)password_verify()is timing-safe- Rehash on login when cost/algorithm changes
- Never use fast hashes like MD5/SHA1 for passwords
More most-asked PHP 8 questions
- What happens to an HTTP request in a PHP-FPM application, and what does 'shared nothing' mean?
- What are the most important features introduced in PHP 8.0?
- What are named arguments and what are their benefits and risks?
- What is constructor property promotion?
- How does `match` differ from `switch`?
- What are union types in PHP 8 and how do you use them?
- What are enums in PHP 8.1? Explain pure vs backed enums.
- Explain readonly properties (8.1) and readonly classes (8.2).