If you're a PHP veteran, of course you know what happens when a php script goes wrong. At this point the PHP parser will give an error message on the screen, such as Fatal error:call to undefined function in line 19--so the program terminates here. This information will scare the customer and he may call you and consult you immediately.
Fortunately, there is a solution. PHP has built-in tools that allow developers to catch script errors and then move them to a custom error handler. At this point, the processor can be programmed to show more specific information about the error. You can also write errors to a file or database to take remedial action. Sometimes you can also ignore error messages for processor writers.
In this article, I'll show you how to use PHP's error handling API to build a user-defined error handler and explain how to display and govern the error message of the script in a simple and friendly manner.
Error type and reporting level
We start from the very basics. PHP has three of the most basic types of errors, from low to advanced: attention, warning, and error (or fatal error). Typically, attention and warnings do not terminate the program; But fatal errors are dangerous failures (for example, calling a function that is not defined or referencing a nonexistent object), which causes the program to break. These errors are likely to occur at startup, parsing, compiling, or running.
Keywords such as e_notice, e_error, etc. are used to indicate the different types and levels of errors. A list of their specific information can be obtained in the PHP manual.
The script stage error display is controlled by the error_reporting () function. This function sets different parameters for different error levels. Table A shows a script that uses this function to report warnings and fatal errors.
Table A
Display warnings and Errors
Error_reporting (e_warning | E_ERROR);
This would generate a notice, which'll never be displayed
Echo $undefinedVar;
This would generate a fatal error, which'll be displayed
Callundeffunc ();
?>
Comparing the code in table B to the above, it is found that Listing B hides the error message and even hides the fatal message so that the error message is not displayed.
Table B
Turn off error display
No errors'll be displayed
error_reporting (0);
This would generate a notice
Echo $undefinedVar;
This would generate a fatal error
Callundeffunc ();
?>
The Code in Table C displays all error messages, even simple attention points:
Table C
All errors'll be displayed
Error_reporting (E_all);
This would generate a notice
Echo $undefinedVar;
This would generate a fatal error
Callundeffunc ();
?>
As shown in the above 3 examples, the error_reporting () function is important to display content on the screen when a control error occurs. The key word here is displayed, which means that the error is not displayed and not the error has not occurred. Therefore, when a fatal error occurs (such as an incorrect function call), the program is terminated, but no message is displayed to the user at this time.
http://www.bkjia.com/PHPjc/632440.html www.bkjia.com true http://www.bkjia.com/PHPjc/632440.html techarticle If you're a PHP veteran, of course you know what happens when a php script goes wrong. At this point the PHP parser will give the error message on the screen, such as Fatal error:call to undefined function o ...