The purpose of this article
Global error Handling in PHP is useful when developing projects, helping developers to quickly locate problems and improve productivity. By default, global errors are output directly, but a framework library used recently for development has set global error handling, causing many error messages to be left out and time-consuming to locate problems. Therefore, the realization of this library is studied, and it is found that it sets error_reporting and Set_error_handler, which leads to this phenomenon. Now record the use of these two functions as a memo.
Background
PHP does not have type detection, it is easier for developers to enter the wrong word, causing fatal errors, and ultimately resulting in the script to stop execution. If you don't get any error messages at this time, it will be a painful thing. You have to start debugging from the first line of the script, and print or echo in the thousands of lines of code until you locate the wrong word. Then you have to go back to the original path and delete all the previously added print or echo. This is a tedious job.
General situation
Under normal circumstances, PHP will be fatal error directly output, will be the wrong source (file address, line number) and cause output, so that the development can be very convenient to locate the problem.
But sometimes, because of the php.ini setup problem, may be the problem of Third-party framework configuration, resulting in this information does 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, which is a bit bit and can be used to set the level of the error output, which is the following copy from the php.ini:
; 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 are warnings which often the result; the bug in your code, but it's possible that it WA s; Intentional (e.g., using a uninitialized variable and relying on the fact it ' s automatically initialized to A; empty string); E_strict-run-time notices, enable to have 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 prints out all error messages except notice. Similarly, the PHP standard function provides the same name function error_reporting (int $level), which is used to accomplish the same function in PHP scripts. This will not affect other programs. It is worth noting that the $level is 0 when the error output is turned off, that is, any errors will not 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 actions, and then you need to customize the error-handling functions. PHP provides built-in functions Set_error_handler can help us register our own error handler functions. The function prototype is as follows:
Mixed Set_error_handler (callback $error _handler [, int $error _types = E_all | E_strict])
It is worth noting that even if the error handler is registered, the default behavior will still be performed, that is, when the error occurs, the error message is still output, so you need to set the error level to 0 in the program, and then register your own error-handling function. This approach is especially important in a production environment because of immediate errors and sensitive internal error messages that are not exposed to potential malicious users. It is also important to note that custom error-handling functions cannot handle fatal errors (such as compilation faults). Here is an application that uses custom error-handling functions:
<?php
error_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
warning:division by Zero In/your/php_demo_fi le.php on line
Fatal Error:trigger a Fatal Error in/your/php_demo_file.php on line 42
As you can see, the last exception to the "New Noexistclass ()" is not captured by a custom error-handling function.
Finally, incidentally, Set_exception_handler registration top-level exception handling, in Web use, you can set up, and then a unified jump to the error-handling page.