What are the best practices for handling error messages in PHP scripts?

When handling error messages in PHP scripts, it is essential to provide clear and informative error messages to aid in debugging and troubleshooting. Best practices include using try-catch blocks to handle exceptions, logging errors to a file or database for future reference, and avoiding displaying sensitive information to users.

try {
    // Your PHP code that may throw an exception
} catch (Exception $e) {
    // Log the error message to a file or database
    error_log("Error: " . $e->getMessage(), 3, "error.log");
    // Display a generic error message to the user
    echo "An error occurred. Please try again later.";
}