What are some best practices for implementing a password change function in PHP with md5 encryption?

When implementing a password change function in PHP with md5 encryption, it is important to follow best practices to ensure the security of user passwords. One key practice is to prompt users to enter their current password before allowing them to change it, to verify their identity. Additionally, the new password should be securely hashed using md5 encryption before storing it in the database.

// Assuming $currentPassword and $newPassword are obtained from user input

// Verify current password
if(md5($currentPassword) == $user['password']) {
    // Hash and update new password
    $hashedPassword = md5($newPassword);
    $query = "UPDATE users SET password = '$hashedPassword' WHERE id = $userId";
    // Execute query to update password in database
} else {
    // Display error message for incorrect current password
}