What are the best practices for handling errors in PHP, particularly when dealing with CMS systems?

When handling errors in PHP, especially in CMS systems, it is important to properly log and display errors to aid in debugging and troubleshooting. One common practice is to set error reporting to display all errors, log them to a file, and show a user-friendly error message to the end user. Additionally, using try-catch blocks for exception handling can help manage errors more effectively.

// Set error reporting to display all errors
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Log errors to a file
ini_set('log_errors', 1);
ini_set('error_log', '/path/to/error.log');

// User-friendly error message
function custom_error_handler($errno, $errstr, $errfile, $errline) {
    echo "An error occurred. Please try again later.";
    error_log("Error: [$errno] $errstr - $errfile:$errline");
}
set_error_handler("custom_error_handler");

// Exception handling with try-catch block
try {
    // Code that may throw an exception
} catch (Exception $e) {
    echo "An error occurred. Please try again later.";
    error_log("Exception: " . $e->getMessage());
}