As long as the program is running, errors will inevitably occur! It's just a matter of time.
Errors are common, such as Notice and Warning. Set_error_handler is generally used for processing:
<? Php
Set_error_handler (function ($ errno, $ errstr, $ errfile, $ errline ){
Var_dump ($ errno, $ errstr, $ errfile, $ errline );
});
// Notice: Use of undefined constant strlen
Strlen;
// Warning: strlen () expects exactly 1 parameter, 0 given
Strlen ();
?>
What can we do? Manage error logs in a unified manner, or display a relatively friendly error prompt page.
Note that set_error_handler cannot catch certain Fatal errors, for example, the following error:
<? Php
Set_error_handler (function ($ errno, $ errstr, $ errfile, $ errline ){
Var_dump ($ errno, $ errstr, $ errfile, $ errline );
});
// Fatal error: Call to undefined function undefined_function ()
Undefined_function ();
?>
But is there no way for us to do it? Of course not. There are not only ways, but also the following:
First: ob_start + error_get_last
<? Php
Ob_start (function ($ buffer ){
If ($ error = error_get_last ()){
Return var_export ($ error, true );
}
Return $ buffer;
});
// Fatal error: Call to undefined function undefined_function ()
Undefined_function ();
?>
Type 2: register_shutdown_function + error_get_last
<? Php
Register_shutdown_function (function (){
If ($ error = error_get_last ()){
Var_dump ($ error );
}
});
// Fatal error: Call to undefined function undefined_function ()
Undefined_function ();
?>
In addition, all Parse errors cannot be captured. However, from a different perspective, the code for parsing errors should not be published, or even be released to the version library, I have previously written an article "Subversion hook", which describes how to use the Subversion hook for code syntax check.
It seems that it is better to write some abnormal text, but it is a pity that it is not too early. Please wash your bed.
Author Wang