What are the differences between encrypting and hashing passwords in PHP?

When storing passwords in a database, it is important to ensure they are properly secured. Encrypting passwords involves encoding the password using a specific algorithm and a key, which can be decrypted to retrieve the original password. Hashing passwords involves using a one-way function to convert the password into a fixed-length string of characters, which cannot be reversed to obtain the original password. Hashing is generally considered more secure than encryption for storing passwords.

// Hashing passwords in PHP
$password = "secretPassword";
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
// Store $hashed_password in the database

// Verifying hashed password
$entered_password = "secretPassword";
if (password_verify($entered_password, $hashed_password)) {
    echo "Password is correct!";
} else {
    echo "Password is incorrect!";
}