What are some best practices for handling errors and displaying error messages in PHP scripts without disrupting the overall functionality of a webpage?

When handling errors in PHP scripts, it is important to display meaningful error messages to users without disrupting the overall functionality of the webpage. One best practice is to use try-catch blocks to catch exceptions and display custom error messages to users. Additionally, logging errors to a file can help developers troubleshoot issues without exposing sensitive information to users.

try {
    // Code that may throw an exception
} catch (Exception $e) {
    // Display a custom error message to the user
    echo "An error occurred. Please try again later.";

    // Log the error to a file for debugging purposes
    error_log($e->getMessage(), 3, "error.log");
}