How can error handling be improved in the PHP script to provide more informative messages?

When handling errors in a PHP script, it is important to provide more informative error messages to help with debugging and troubleshooting. One way to improve error handling is to use the try-catch block to catch exceptions and then display a custom error message with more details about the issue.

try {
    // code that may throw an exception
    $result = 1 / 0;
} catch (Exception $e) {
    // display a custom error message with details about the exception
    echo "An error occurred: " . $e->getMessage();
}