What are some best practices for managing multiple password hashing algorithms in a PHP application to accommodate different user authentication methods?

When managing multiple password hashing algorithms in a PHP application to accommodate different user authentication methods, it is important to securely store the algorithm identifier along with the hashed password. This allows the application to determine the correct algorithm for verifying passwords during the authentication process. One approach is to store the algorithm identifier and hashed password together in a single string, separated by a delimiter, and then use this information to verify passwords based on the specified algorithm.

// Example of storing hashed password with algorithm identifier
$algorithm = 'bcrypt'; // Algorithm identifier
$password = 'user_password';
$hashedPassword = password_hash($password, PASSWORD_DEFAULT); // Hash password using default algorithm

// Store hashed password and algorithm identifier together
$hashedPasswordWithAlgorithm = $algorithm . ':' . $hashedPassword;

// Verify password using stored algorithm identifier
list($storedAlgorithm, $storedHash) = explode(':', $hashedPasswordWithAlgorithm, 2);
if (password_verify($password, $storedHash) && $storedAlgorithm === $algorithm) {
    // Password is valid
    echo 'Password is valid';
} else {
    // Password is invalid
    echo 'Password is invalid';
}