What are the best practices for comparing hashed passwords in PHP to ensure secure authentication?

When comparing hashed passwords in PHP for secure authentication, it is essential to use a secure hashing algorithm like bcrypt and to verify the passwords using the password_verify function. This function compares a plaintext password with a hashed password securely, preventing timing attacks and ensuring a more robust authentication process.

$plaintextPassword = "password123";
$hashedPasswordFromDatabase = "$2y$10$V9d1kPw7qV1YdR6wY1T3kOKFz6xN5Y7kzU7QsP6zWuRt4Qw6zGQ2K";

if (password_verify($plaintextPassword, $hashedPasswordFromDatabase)) {
    echo "Password is correct!";
} else {
    echo "Password is incorrect!";
}