PHP Global error handling, PHP global explanation
Purpose of this article
PHP's global error handling, when developing projects, is useful to help developers quickly locate problems and improve productivity. By default, global errors are output directly, but a framework library used recently for development has set up global error handling, causing many error messages to be left out and time-consuming to locate the problem. Therefore, this paper studies the implementation of this library, found that it set the error_reporting and Set_error_handler, resulting in this phenomenon. Now record the usage of these two functions as a memo.
Background
PHP does not have type detection, and developers are more likely to enter incorrect words, causing fatal errors and eventually causing the script to stop executing. If you don't get any error messages at this time, it's going to be a very painful thing. You have to start debugging from the first line of the script, and print or echo in thousands of lines of code until you navigate to the wrong word. Then, there has to be a return to the original, removing all previously added print or echo. This is a dull job.
General situation
Normally, PHP will directly output the fatal error, the source of the error (file address, line number) and the reason for output, so that the development can be very convenient to locate the problem.
However, sometimes, because of the php.ini setup problem, may be a third-party framework configuration problems, resulting in this information is not output, then you must learn to set the relevant parameters, output these error messages, to help quickly locate the problem.
Error_reporting
Error_reporting is a global configuration parameter for PHP, in php.ini. Used to configure the error output level, the parameter is a bit bit, can be used to set the level of error output, the following is the information from php.ini copy:
; Error_reporting is a bit-field. Or each number up to get desired error; Reporting level; E_all-all errors and warnings (doesn ' t include e_strict); E_error-fatal run-time errors; E_recoverable_error-almost Fatal run-time errors; E_warning-run-time warnings (non-fatal errors); E_parse-compile-time PARSE errors; E_notice-run-time Notices (these is warnings which often result; from a bug in your code, but it's possible that it was ; Intentional (e.g., using a uninitialized variable and; Relying on the fact it's automatically initialized to an; empty St Ring); E_strict-run-time notices, enable to has PHP suggest changes; To your code which would ensure the best interoperability; And forward compatibility of your code; E_core_error-fatal errors that occur during PHP ' s initial startup; E_core_warning-warnings (non-fatal errors) that occur during PHP ' s; Initial startup; E_compile_error-fatal compile-time errors; E_compile_warning-compile-time warnings (non-fatal errors); e_user_error-user-generated ERROR message; e_user_warning-user-generated WARNING message; e_user_notice-user-generated NOTICE message;; Examples:;; -Show all errors, except for notices and coding standards warnings; error_reporting = E_all & ~e_notice;; -Show all errors, except for notices;; error_reporting = e_all & ~e_notice | E_strict;; -Show only errors;; error_reporting = e_compile_error| e_recoverable_error| e_error| E_core_error;; -Show all errors except for notices and coding standards Warnings;error_reporting = E_all & ~e_notice
By default, PHP will output all error messages except notice. Similarly, a function error_reporting (int $level) with the same name is provided in the PHP standard function, which is used in PHP scripts to accomplish the same function. This will not affect other programs. It is worth noting that when the $level is 0, the error output is turned off, which means that no errors are output.
Set_error_handler
The default error handling for PHP is to output the message. However, there are times when you need to define some other action, and you need to customize the error handling function. PHP provides built-in function Set_error_handler to help us register our own error-handling functions. The function prototypes are as follows:
Mixed Set_error_handler (callback $error _handler [, int $error _types = E_all | E_strict])
It is important to note that even if the error handler is registered, the default behavior will still be executed, that is, when the error occurs, the error message will still be output, so you need to set the error level to 0 in the program, and then register your own error-handling function. This is especially important in a production environment, where instant errors occur and sensitive internal error messages are not exposed to potentially malicious users. It is also important to note that custom error handlers cannot handle fatal error (such as compilation errors). The following is an argument that uses a custom error-handling function:
<?phperror_reporting (0); function Error_Handler ($error _level, $error _message, $file, $line) { $EXIT = FALSE; Switch ($error _level) {case e_notice: Case e_user_notice: $error _type = ' NOTICE '; break; Case e_warning: Case e_user_warning: $error _type = ' WARNING '; break; Case E_error: Case e_user_error: $error _type = ' Fatal ERROR '; $EXIT = TRUE; break; Default: $error _type = ' Unknown '; $EXIT = TRUE; break; } printf ("%s:%s in '%s on line%d\n", $error _type, $error _message, $file, $line); if ($EXIT) {die (); }} Set_error_handler (' Error_Handler '); New NonExist (); Echo $novar; Echo 3/0;trigger_error (' Trigger a fatal error ', e_user_error); new NonExist (); >
Execute this script to get the following output:
notice:undefined Variable:novar in/your/php_demo_file.php on line 40warning:division by zero in/your/php_demo_file.ph P on line 41Fatal Error:trigger a fatal Error in/your/php_demo_file.php on line 42
As you can see, the last exception for "New Noexistclass ()" is not captured by the custom error handler.
Finally, incidentally, Set_exception_handler register the top layer of exception handling, in the Web one use, you can set a bit, and then a unified jump to the error handling page.
http://www.bkjia.com/PHPjc/1123844.html www.bkjia.com true http://www.bkjia.com/PHPjc/1123844.html techarticle PHP Global error handling, PHP Global details of the purpose of the PHP global error handling, in the development of the project is very useful, can help developers quickly locate some problems, improve the work ...