How can error reporting be optimized in PHP scripts to catch and debug issues more effectively?

To optimize error reporting in PHP scripts, you can enable error reporting, set error reporting level to catch all types of errors, log errors to a file or display them on the screen, and handle errors gracefully with custom error handling functions.

// Enable error reporting
error_reporting(E_ALL);

// Set error reporting level
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

// 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) {
    echo "Error: [$errno] $errstr - $errfile:$errline";
    error_log("Error: [$errno] $errstr - $errfile:$errline", 0);
}

set_error_handler("customErrorHandler");