How can error reporting be optimized in PHP scripts to identify and resolve issues?

To optimize error reporting in PHP scripts, you can enable error reporting, display errors, log errors, and handle errors gracefully to identify and resolve issues more efficiently.

// Enable error reporting
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');

// Custom error handling function
function customErrorHandler($errno, $errstr, $errfile, $errline) {
    // Handle errors gracefully
    echo "Error: [$errno] $errstr - $errfile:$errline";
    // Log errors
    error_log("Error: [$errno] $errstr - $errfile:$errline", 3, "/path/to/error.log");
}

// Set custom error handler
set_error_handler("customErrorHandler");