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.
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('errorHandler'); register_shutdown_function( 'close' ); function 大专栏 php捕获致命错误class="hljs-title">close(){ ob_end_clean(); $error = error_get_last(); echo '<pre class="brush:php;toolbar:false">'; print_r($error); } require 'short.php';
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!