PHP register_shutdown_function function Use detailed

Source: Internet
Author: User
Tags php error
This article mainly introduces the PHP error handling function Register_shutdown_function Use example, the need for friends can refer to the following

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 ().

Can be called multiple times register_shutdown_function() , 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_functionfunction, our function will be called when the execution of our script or accidental death causes PHP execution to close, and 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 Reporting error_reporting (0);//implement your own error message to show Register_shutdown_function (' Myshutdown '); $debug = True;function Myshutdown () {  global $debug;  Whether or not the error occurs, this sentence will execute  Echo ' error ', ' <br/> ';  if (! $debug) {    $error = Error_get_last ();    Todo can do a mail send alert or error log collection    Var_export ($error) here;}  }

DEMO2

Back to function with parameters: Log 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

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.