Home > Backend Development > PHP Tutorial > How to Handle Fatal PHP Errors (E_ERROR) in PHP 5.2 ?

How to Handle Fatal PHP Errors (E_ERROR) in PHP 5.2 ?

Mary-Kate Olsen
Release: 2024-12-19 17:01:13
Original
278 people have browsed it

How to Handle Fatal PHP Errors (E_ERROR) in PHP 5.2 ?

Handling Fatal PHP (E_ERROR) Errors

Problem:

If a fatal error occurs in a PHP script, such as calling a non-existent function, the error cannot be caught using the set_error_handler() function. How can you handle these critical errors?

Solution:

To catch fatal errors in PHP 5.2 , utilize the register_shutdown_function() function:

register_shutdown_function("fatal_handler");

function fatal_handler() {
    $errfile = "unknown file";
    $errstr = "shutdown";
    $errno = E_CORE_ERROR;
    $errline = 0;

    $error = error_get_last();

    if ($error !== NULL) {
        $errno = $error["type"];
        $errfile = $error["file"];
        $errline = $error["line"];
        $errstr = $error["message"];

        error_mail(format_error($errno, $errstr, $errfile, $errline));
    }
}
Copy after login

You need to implement the error_mail() and format_error() functions, for instance:

function format_error($errno, $errstr, $errfile, $errline) {
    $trace = print_r(debug_backtrace(false), true);

    $content = "
    <table>
        <thead><th>Item</th><th>Description</th></thead>
        <tbody>
            <tr>
                <th>Error</th>
                <td><pre class="brush:php;toolbar:false">$errstr
Errno
$errno
File $errfile Line $errline Trace
$trace
"; return $content; }
Copy after login

For sending emails, employ Swift Mailer to define the error_mail() function.

Additional Resources:

  • $php_errormsg
  • Predefined Constants

The above is the detailed content of How to Handle Fatal PHP Errors (E_ERROR) in PHP 5.2 ?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template