How to catch fatal errors in php

藏色散人
Release: 2023-03-02 22:48:01
Original
3143 people have browsed it

php method to get fatal errors: first open "ob_start" and create a "tmp.php" file; then write some error codes in the "short.php" file; finally access "tmp.php" Just file.

How to catch fatal errors in php

php captures fatal errors

Recording php error logs can effectively help us find problems and fix bugs, php Set_error_handler and set_exception_handler are provided to capture errors and exceptions. However, set_error_handler cannot capture E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING, E_STRICT and other errors. Generally speaking, if it is a fatal error, it will cause the PHP interpreter to fail to compile. Naturally, these errors cannot be caught. However, we can use some methods to cleverly obtain these error messages.

Recommended: "PHP Tutorial"

First create a tmp.php

<?php
//error_reporting(0);
ob_start();
set_error_handler(&#39;errorHandler&#39;);
register_shutdown_function( &#39;close&#39; );
function 大专栏  php捕获致命错误class="hljs-title">close(){
ob_end_clean();
$error = error_get_last();
echo &#39;<pre class="brush:php;toolbar:false">&#39;;
print_r($error);
}
require &#39;short.php&#39;;
Copy after login

and then write some error codes in short.php, For example,

echo $a//deliberately not adding a semicolon

and then accessing tmp.php, you will find that the page does not display the error message of php itself, but executes the close method. There are a few key points in the above code.

First, turn on ob_start

Second: Use register_shutdown_function to register the function

Third: Use the error_get_last function to obtain error information

Fourth: PHP parsing The first file cannot have a fatal error, otherwise it cannot be captured. Therefore, it is also recommended that when you build your own framework or choose a framework, it is best to use the single entry method

The above is the detailed content of How to catch fatal errors in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template