Friday, 18 October 2024

How to Catch a PHP Fatal (E_ERROR) Error

In PHP, fatal errors (such as E_ERROR) can cause a script to terminate immediately, making it challenging to capture and handle these errors. While PHP’s set_error_handler() function allows catching many types of errors, it doesn’t work for fatal errors. In this post, we’ll explore different ways to handle fatal errors effectively, especially in older versions of PHP, and how PHP 7+ offers a more structured approach to error handling.

Problem with Catching Fatal Errors

Fatal errors in PHP, like calling a non-existent function or running out of memory, cannot be caught by set_error_handler() because these errors cause the script to terminate before reaching the handler. For example, this code will not catch a fatal error:

Read more »

Labels: