What is the best practice for error handling in PHP scripts to ensure accurate error messages are displayed to users?

When handling errors in PHP scripts, it is important to use try-catch blocks to catch and handle exceptions gracefully. This ensures that accurate error messages are displayed to users without revealing sensitive information about the code or system. By using try-catch blocks, you can control how errors are handled and provide meaningful feedback to users.

try {
    // code that may throw an exception
} catch (Exception $e) {
    // handle the exception, log it, and display a user-friendly error message
    echo "An error occurred: " . $e->getMessage();
}