How can error reporting be optimized for PHP code to identify issues more effectively?

To optimize error reporting for PHP code and identify issues more effectively, you can enable error reporting, set error reporting level to display all types of errors, log errors to a file, and handle errors gracefully by displaying user-friendly error messages.

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

// Handle errors gracefully
set_error_handler(function($errno, $errstr, $errfile, $errline) {
    echo "Error: [$errno] $errstr - $errfile:$errline";
    // You can log errors to a file or send an email notification here
});

// Your PHP code goes here