What are the best practices for error handling in PHP to avoid displaying server details to users?

When handling errors in PHP, it is important to avoid displaying detailed server information to users as it can pose a security risk. To prevent this, you can use PHP's error handling functions such as `set_error_handler` and `set_exception_handler` to customize error messages and log errors without revealing sensitive information.

// Set custom error handler to log errors without displaying server details
function customErrorHandler($errno, $errstr, $errfile, $errline) {
    error_log("Error: [$errno] $errstr in $errfile on line $errline");
    // Display a generic error message to the user
    echo "An error occurred. Please try again later.";
}

set_error_handler("customErrorHandler");

// Example code that may trigger an error
$file = 'non_existent_file.txt';
if (!file_exists($file)) {
    trigger_error("File '$file' not found", E_USER_ERROR);
}