What are the recommended methods for handling salts and encryption algorithms in PHP when updating passwords in a database for improved security?

When updating passwords in a database for improved security, it is recommended to use salts and encryption algorithms to store passwords securely. Salting involves adding a random string of characters to the password before hashing it, which adds an extra layer of security. Encryption algorithms like bcrypt or Argon2 should be used for hashing passwords, as they are designed to be slow and resistant to brute force attacks.

// Generate a random salt
$salt = bin2hex(random_bytes(16));

// Combine the password with the salt and hash it using bcrypt
$hashed_password = password_hash($password . $salt, PASSWORD_BCRYPT);

// Store the hashed password and salt in the database
$stmt = $pdo->prepare("UPDATE users SET password = :password, salt = :salt WHERE id = :id");
$stmt->execute(array(':password' => $hashed_password, ':salt' => $salt, ':id' => $user_id));