What are the best practices for securely storing and comparing passwords in PHP?

Storing and comparing passwords securely in PHP involves using a strong hashing algorithm like bcrypt, salting the passwords before hashing, and using a secure comparison function to verify passwords. This helps protect against common attacks like brute force and rainbow table attacks.

// Storing a password securely
$password = 'password123';
$hashedPassword = password_hash($password, PASSWORD_BCRYPT);

// Comparing a password securely
$userPassword = 'password123';
if (password_verify($userPassword, $hashedPassword)) {
    // Passwords match
    echo 'Password is correct';
} else {
    // Passwords do not match
    echo 'Password is incorrect';
}