Scripts often die, and they don't always look good. We don't want to show the user a fatal error, or a blank page (when display_errors is set to off). There is a function in PHP called register_shutdown_function that allows We set up another function that can be called when the execution is closed.That is to say, when our script execution is completed or unexpectedly dies, causing the PHP execution to be shut down, our function will be called. So, we can use the method of setting a variable to false at the beginning of the script, and then setting it to true at the end of the script to let PHP close the callback function to check whether the script is completed. If our variable is still false, we know that the last line of the script was not executed, so it must have died somewhere in the execution of the program. I have prepared a very basic example to demonstrate how you should give the user something when a fatal error needs to be displayed. Appropriate feedback. You can make the example look better by turning off the display of fatal errors. As follows:
$clean = false;function shutdown_func(){global $clean;
if (!$clean){
die("not a clean shutdown");
}
return false;
}
register_shutdown_function("shutdown_func");
$a = 1;
$a = new FooClass(); // Will fail with fatal error
$clean = true;
?>
As you can see, if the callback function is turned off when running, the clean variable If not set to true, the shutdown_func function will print something. This thing can be packaged into a class (without using global variables).
PHP provides the register_shutdown_function() function, which can call back the registered function before the script terminates. That is, the function that is executed when the PHP program is completed.
register_shutdown_function The execution mechanism is: PHP transfers the function to be called into memory. This function is called again when all PHP statements on the page have been executed. Note that at this time it is called from memory, not from the PHP page, so the above example cannot use relative paths because PHP has already assumed that the original page does not exist. There is no relative path at all.
Note: register_shutdown_function
means calling the function after all PHP statements have been executed. Do not understand it as calling the function when the client closes the streaming browser page.
You can understand the calling conditions like this:
1. When the page is forced to stop by the user 2. When the program code runs timeout
3. When the PHP code is executed When completed, there are exceptions, errors, and warnings in code execution
http://www.bkjia.com/PHPjc/327395.html
www.bkjia.com
true
http: //www.bkjia.com/PHPjc/327395.htmlTechArticleScripts often die, and they don’t always look good. We don’t want to show a fatal error to the user, and Or a blank page (when display_errors is set to off). There is a page in PHP called...
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn