What are some common PHP functions used for encoding and encrypting text, especially for password protection?

When storing passwords or sensitive information in a database, it is crucial to encode or encrypt the text to ensure security. PHP provides several functions for encoding and encrypting text, such as password_hash() for hashing passwords using a strong one-way hashing algorithm, and password_verify() for verifying hashed passwords. Additionally, the openssl_encrypt() and openssl_decrypt() functions can be used for encrypting and decrypting text using symmetric encryption algorithms.

// Example of hashing a password using password_hash()
$password = "secret_password";
$hashed_password = password_hash($password, PASSWORD_DEFAULT);

// Example of verifying a hashed password using password_verify()
$entered_password = "secret_password";
if (password_verify($entered_password, $hashed_password)) {
    echo "Password is correct!";
} else {
    echo "Password is incorrect!";
}

// Example of encrypting and decrypting text using openssl_encrypt() and openssl_decrypt()
$text = "sensitive_information";
$encryption_key = "encryption_key";
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc'));
$encrypted_text = openssl_encrypt($text, 'aes-256-cbc', $encryption_key, 0, $iv);
$decrypted_text = openssl_decrypt($encrypted_text, 'aes-256-cbc', $encryption_key, 0, $iv);