System error handler:
Under normal circumstances in PHP, errors will be output normally, but in some frameworks, it may affect the error output. It may be that the framework itself has its own processing mechanism, or it may It is processed in the code, usually these function settings:
1.error_reporting(); Set the error level of PHP and return the current level
error_reporting(report_level)
If the parameter level is not specified, the current error level will be returned. The following items are possible values for level:
Value |
Constant |
Description |
##1 | E_ERROR | ##fatal Runtime error. This error cannot be recycled. Script execution was interrupted.|
E_WARNING | Non-fatal runtime warning . Script execution is not interrupted. | |
##E_PARSE | Compile time analysis error. Parsing should only generate errors |
|
8 |
E_NOTICE |
Runtime notification. The script found may be an error, but typically when running a script, |
16 |
E_CORE_ERROR may also occur |
Fatal error when starting PHP. This is like E_ERROR |
32 |
E_CORE_WARNING |
# in PHP core ## Warn on PHP startup. This is like E_WARNING |
#64 | ##E_COMPILE_ERROR# in the PHP core ##Fatal compile-time error. This is like the E_ERROR | generated by the Zend script engine|
128 |
E_COMPILE_WARNING |
Non-fatal compile-time warning. This is like generating E_WARNING |
256 |
E_USER_ERROR | ## through the Zend script engine. # Fatal user-generated error, similar to E_ERROR set by the programmer using the PHP function trigger_error() |
| E_USER_WARNINGNon-fatal user-generated warning, similar to the E_WARNING set by the programmer using the PHP function trigger_error | |
E_USER_NOTICE | A user-generated notification, similar to the E_NOTICE set by the programmer using the PHP function trigger_error | |
E_STRICT |
Runtime notification. PHP recommends changes to your code to aid interoperability and compatibility of this code |
|
4096 |
E_RECOVERABLE_ERROR |
Catchable fatal error, similar to E_ERROR, but can be caught by a user-defined handler (see set_error_handler()) |
8191 |
#E_ALL |
All errors and warnings except level E_STRICT (in PHP6.0, E_STRICT will be E_ALL a part of) |
It is worth noting here that when $level is 0, error output is turned off, that is, no errors will be output.
2.set_error_handler()
Definition and usage
The set_error_handler() function sets a user-defined error handling function.
This function is used to create the user's own error handling method during runtime.
This function will return the old error handler, or null if it fails.
Syntax
set_error_handler(error_function,error_types)
Parameters |
Description |
error_function |
Required. Specifies the function to run when an error occurs. |
error_types |
Optional. Specifies at which error reporting level user-defined errors are displayed. The default is "E_ALL". |
# Tip: If this function is used, the standard PHP error handling function will be completely bypassed, and the user-defined error handler must terminate if necessary (die () ) Script,
Note: If an error occurs before the script is executed, the custom error handler will not be used because the custom program has not been registered at that time.
The test code is as follows:
参数 | 描述 |
error_message | 必需。规定错误消息。长度限制为 1024 个字符。 |
error_types | 可选。规定错误消息的错误类型。 可能的值:
|
测试代码如下:
/** * * @param type $level * @param type $msg */ function my_error($level, $msg) { switch ($level) { case E_USER_ERROR: echo "ERROR:<br data-filtered="filtered">"; break; case E_USER_WARNING: echo "WARNING:<br data-filtered="filtered">"; break; case E_USER_NOTICE: echo "NOTICE:<br data-filtered="filtered">"; break; default: break; } echo "错误编号:" . $level . " <br data-filtered="filtered">"; echo "错误信息:" . $msg; } //注册错误处理器 set_error_handler('my_error'); if (89 > 8) { //调用错误触发器 trigger_error('这是错误啊', E_USER_WARNING); } Copy after login |
运行结果如下:
WARNING:
错误编号:512
错误信息:这是错误啊
相关推荐:
The above is the detailed content of PHP error handling method example. For more information, please follow other related articles on the PHP Chinese website!