PHP Error handling function register_shutdown_function

Source: Internet
Author: User

When the program is running online, if you encounter a bug, want to not output error messages in the front end, but also timely email to inform the developer, Register_shutdown_function function can come in handy.

Registers a function that will be called after script execution completes or exit ().

Register_shutdown_function () can be called multiple times , and these registered callbacks are called sequentially in the order in which they were registered. If you call exit () inside a registered method , all processing is aborted, and the other registered abort callbacks are no longer called.

register_shutdown_function function, our function will be called when our script executes or the accidental death causes PHP execution to close, we can use it with Error_get_last to get the error message.

register_shutdown_function(Callable$callback[,Mixed $parameter[,Mixed$...] ]) callable callback function parameter can pass arguments to the abort function by passing in additional parameters DEMO1:
//Turn off Error reportingerror_reporting(0);//implement your own error message displayregister_shutdown_function(' Myshutdown ');$debug=true;functionMyshutdown () {Global $debug; //Whether or not the error occurred, this sentence will be executed Echo' ERROR ', ' <br/> '; if(!$debug) { $error=Error_get_last (); //Todo can do email reminders or error log collection here Var_export($error); }}
DEMO2
//back to function with parameters: Record the current request URL$current _page=Htmlspecialchars($_server[' Script_name '], ent_quotes, ' UTF-8 ');$current _page.=$_server[' Query_string ']? ‘?‘.Htmlspecialchars($_server[' Query_string '], ent_quotes, ' UTF-8 '): ';register_shutdown_function(function($current _page) { //todo send email or log},$current _page);Error_get_last ()
Error message View:
http//php.net/manual/zh/errorfunc.constants.php

PHP error handling function register_shutdown_function

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.