How can you improve the error handling and feedback for password update operations in PHP?

Issue: To improve error handling and feedback for password update operations in PHP, you can implement checks for password strength requirements, provide informative error messages for invalid inputs, and use try-catch blocks to handle exceptions gracefully.

<?php
// Check if the password meets the strength requirements
function validatePassword($password) {
    if (strlen($password) < 8) {
        throw new Exception("Password must be at least 8 characters long");
    }
}

// Update password function with error handling
function updatePassword($userId, $newPassword) {
    try {
        validatePassword($newPassword);
        
        // Update password in the database
        // Your database update code here
        
        echo "Password updated successfully";
    } catch (Exception $e) {
        echo "Error updating password: " . $e->getMessage();
    }
}

// Example usage
$userId = 1;
$newPassword = "weak";
updatePassword($userId, $newPassword);
?>