What is the difference between hashing and encrypting passwords in PHP?

Hashing passwords in PHP involves using a one-way cryptographic hash function to convert the password into a fixed-length string of characters. This process is irreversible, meaning the original password cannot be retrieved from the hash. Encrypting passwords, on the other hand, involves using an encryption algorithm to convert the password into a ciphertext that can be decrypted back to the original password. To securely store passwords in PHP, it is recommended to hash them using a strong hashing algorithm like bcrypt. This ensures that even if the hashed passwords are compromised, they cannot be easily reversed to obtain the original passwords. Encrypting passwords is not recommended for storing passwords as it introduces the risk of decryption and exposes the original passwords if the encryption key is compromised.

// Hashing a password using bcrypt
$password = "secretPassword";
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);

// Verifying a password
$enteredPassword = "secretPassword";
if (password_verify($enteredPassword, $hashedPassword)) {
    echo "Password is correct";
} else {
    echo "Password is incorrect";
}