PHP error handling

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.