Home > Article > Backend Development > php error handling and implementation - CSDN Blog
This article introduces the error handling and implementation of PHP. Now I will share it with you. If you need it, you can refer to
1.php's errors, warnings, and exception handling. How to implement them? The function is used to handle the corresponding error
php Exception prompts are divided into three categories error/warning/notice
Error supplement:
Fatal Error: Fatal error (script Terminate running)
E_ERROR // Fatal running error, error cannot be recovered, pause execution of script
E_CORE_ERROR // Fatal error during initialization process when PHP starts
E_COMPILE_ERROR // Fatal error during compilation, like An E_ERROR
E_USER_ERROR // custom error message is generated by the Zend script engine. Like using the PHP function trigger_error (the error type is set to: E_USER_ERROR)
E_RECOVERABLE_ERROR //Fatal errors that can be captured. It indicates that a potentially dangerous error has occurred, but has not caused the PHP engine to become unstable.
Parse Error: Compilation time parsing error, syntax error (script terminates running)
E_PARSE //Syntax parsing error during compilation
Warning supplement: Warning error (only prompt information is given, the script does not terminate running)
E_WARNING // Runtime warning (non-fatal error).
E_CORE_WARNING // Warning (non-fatal error) that occurs during PHP initialization startup.
E_COMPILE_WARNING // Compilation warning
E_USER_WARNING // Warning message generated by the user
Exception notification supplement: notification error (only notification information is given, the script does not terminate the operation)
E_NOTICE // Runtime notification. Indicates that the script encounters a situation that may appear as an error.
E_USER_NOTICE // Notification information generated by the user.
php error handling implementation:
register_shutdown_function('funcName') // The parameter is the name of the function that needs to capture errors, but register_shutdown_function can only capture fatal errors at runtime Errors cannot capture execution errors during interpretation, because they are functions executed after the program execution is completed (such as syntax errors, errors that occur during runtime will not be captured)
set_error_handler('funcName') //The parameter is the function name of the callback that needs to be captured. It is different from register_shutdown_function, which captures errors during the running of the function.
When the target file encounters an error during execution, the system will call back the specified function:
Example:
<?php register_shutdown_function( "fatal_handler" ); set_error_handler("error_handler"); define('E_FATAL', E_ERROR | E_USER_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_RECOVERABLE_ERROR| E_PARSE ); //获取fatal error function fatal_handler() { $error = error_get_last(); if($error && ($error["type"]===($error["type"] & E_FATAL))) { $errno = $error["type"]; // $errfile = $error["file"]; $errline = $error["line"]; $errstr = $error["message"]; error_handler($errno,$errstr,$errfile,$errline); } } //获取所有的error function error_handler($errno,$errstr,$errfile,$errline){ $str=<<<EOF "errno":$errno "errstr":$errstr "errfile":$errfile "errline":$errline EOF; //获取到错误可以自己处理,比如记Log、报警等等 echo $str; } //error_get_last() 捕获到的执行错误结果 //error_clear_last() 清除最后一次产生的错误信息 //上面两者可以捕获所有错误,警告, 异常,但作性能考虑,不建议使用其捕获警告异常
Related recommendations:
A brief analysis of PHP error handling, automatic loading, stack Memory and running mode
PHP error handling instance method
PHP error and exception debugging video tutorial resource sharing
##
The above is the detailed content of php error handling and implementation - CSDN Blog. For more information, please follow other related articles on the PHP Chinese website!