Type of error
There are two main types of errors in PHP: triggering errors and exceptions. The triggering errors can be categorized as: compilation errors, engine errors, and run-time errors, the first two of which cannot be captured, and exceptions can be captured, and the code is interrupted when there is no attempt to capture.
Triggering errors can be obtained by means of which error_get_last()
exceptions can be captured using standard try...catch
statements.
Handling Error Reporting
The error_reporting () function is a function that PHP provides to control the level of error, and if the corresponding error level is set, only the error message of the response will be output on the page (or output, for the command line) when the corresponding error level is triggered.
The error_reporting (int level) support levels parameter uses a binary number to configure the error level, and the error level generally replaces the corresponding binary identifier with a constant. For example: E_all, E_notice, E_user_notice, and so on; If the parameter is not passed, the current error level is returned (typically used to hold the current error level for subsequent recovery).
Where E_parse, e_compile_*, and e_core_* are non-run-time errors that cannot be captured
Ini
The configuration file is the PHP initialization 默认
configuration, and the corresponding initialization parameters can be controlled by modifying some parameters. If you want to control errors, you can configure them in several ways:
Display_errors-whether an error is displayed, typically in a production environment it is recommended to close the parameter and try it together with the following parameters log_errors-Log the error switch if Open will be recorded to the corresponding location Error_log-the location of the error log is not specified as the system default error _reporting-the same function as above
Error suppression
PHP provides a special way to suppress an error by adding a suppress error operator to the statement before @
the statement. This is typically used when you do not know what will happen, such as opening an indeterminate file or a Web URL, but it is generally not recommended
Suppress the error itself within a series of processing to achieve this effect, and for the error itself is completely unknown, in general, it is not recommended to use
Catching exceptions
In almost all languages, there is a handling of exceptions. If the program throws an exception, you can catch the exception through the Try...catch statement, or you can only catch errors of that type if you explicitly know the type of the error. Try...catch is one of the most standard error handling methods
1 2 3 4 5 |
try { // 代码部分} catch (Exception $e) { // 根据异常对象对 $e 进行处理}
|
Setting the error handler
PHP provides the Set_error_handler () function to allow developers to pass in a function name (or anonymous function, for PHP 5.2 or more) to get all the errors into the function, and then the developer can control within the function, for example
1 2 3 |
set_error_handler(function($code, $msg){ log_error("$msg occur with code $code");});
|
Correspondingly, PHP also provides restore_error_handler () to revert to the standard PHP error control.
Setting the exception handler
If every exception is captured, it is obviously a complex thing to write a lot of code, and it will be more complex to maintain, and for a fast programming language, PHP will not allow this to happen naturally. Using Set_exception_handler (), you can receive all the uncaught exceptions like the above error-handling controller, and then do something that you can do, and even throw out the errors. For example:
1 2 3 |
set_exception_handler(function($exception){ log_error("Exception occur: $exception");});
|
Crash capture
This kind of processing is not the PHP standard processing method, but for some kind of failure to make a program can not catch the error is very useful, it refers to the registration of a shutdown function, within the function by calling Error_get_last () to obtain the final error, and according to the error level to do the corresponding processing.
1 2 3 4 5 6 |
register_shutdown_function(function(){ $error = error_get_last(); if ($error && $error[‘code‘]()() & error_reporting()) { // 崩溃错误,记录日志 }});
|