If you are a veteran of PHP, you certainly know what happens when a PHP script error occurs. At this point, the PHP parser will give an error message on the screen, such as Fatal error: Call to undefined function on line 19 --, so the program ends here. This information will scare the customer, and he may immediately call you for consultation.
Fortunately, there is a solution here. PHP has built-in tools that allow developers to capture script errors and then transfer them to custom error processors. At this time, you can program the processor to display more details about the error. You can also write errors to files or databases to take remedial measures. Sometimes you can write programs on the processor to ignore error messages.
In this article, I will explain how to use the PHP error processing API to build a custom error processor, and how to display and govern script error messages in a simple and friendly way.
Error Type and report level
We start from the most basic. PHP has three basic error types: attention, warning, and error (or fatal error), from low to advanced ). In general, attention and warning will not terminate the program, but a fatal error is a dangerous fault (for example, calling an undefined function or referring to a non-existent object), which will lead to program interruption. These errors may occur during startup, parsing, compilation, or running.
Key words such as E_NOTICE and E_ERROR are used to indicate different types and levels of errors. You can obtain a list of specific information about the PHP Manual.
The error display in the script stage is controlled by the error_reporting () function. This function sets different parameters for different error levels. Table A provides script programs that report warnings and fatal errors using this function.
Table
<? Php
// Display warnings and errors
Error_reporting (E_WARNING | E_ERROR );
// This will generate a notice, which will never be displayed
Echo $ undefinedVar;
// This will generate a fatal error, which will be displayed
CallUndefFunc ();
?>
Compare the code in Table B with the above code and find that the error information in Listing B is hidden or even fatal, so that the error information is not displayed.
Table B
<? Php
// Turn off error display
// No errors will be displayed
Error_reporting (0 );
// This will generate a notice
Echo $ undefinedVar;
// This will generate a fatal error
CallUndefFunc ();
?>
The code in Table C shows all error messages and even simple precautions:
Table C
<? Php
// All errors will be displayed
Error_reporting (E_ALL );
// This will generate a notice
Echo $ undefinedVar;
// This will generate a fatal error
CallUndefFunc ();
?>
As shown in the preceding three examples, the error_reporting () function is very important to display content on the screen when a control error occurs. The keyword displayed indicates that the error is not displayed, rather than the error. Therefore, when a fatal error occurs (for example, an incorrect function call), the program is terminated. However, no message is displayed to the user.