What are the advantages of using password_hash() and password_verify() functions in PHP for securely storing and comparing passwords?

Storing passwords securely is crucial to protect user data from unauthorized access. Using the password_hash() function in PHP allows for secure hashing of passwords, making it difficult for attackers to reverse engineer the original password. The password_verify() function then enables secure comparison of a user-entered password with the hashed password stored in the database, ensuring authentication without exposing the actual password.

// Storing a password securely using password_hash()
$password = "secretPassword";
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);

// Comparing a user-entered password with the hashed password using password_verify()
$userInput = "secretPassword";
if (password_verify($userInput, $hashedPassword)) {
    echo "Password is correct!";
} else {
    echo "Password is incorrect!";
}