PHP register_shutdown_function details

Source: Internet
Author: User

Scripts often die and are not always so nice. We don't want to show users a fatal error or a blank page (when display_errors is set to off ).
 
PHP has a function called register_shutdown_function. We can set another function that can be called when the function is disabled. that is to say, when the execution of our script is completed or accidentally killed, and PHP Execution is about to be disabled, our function will be called. therefore, we can set a variable to false at the beginning of the script, and then set it to true at the end of the script, so that PHP can disable the callback function to check whether the script is complete or not.
 
If our variable is still false, we will know that the last line of the script is not executed, so it will surely die somewhere when the program is executed. I have prepared a very basic example to demonstrate how to give users proper feedback when a fatal error needs to be displayed. you can turn off the display of fatal errors (you can set display_errors and error_reporting) to make the example look better.
 
<? Php
 
$ 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 due to a fatal error $ clean = true;?>
 
As you can see, if the clean variable is not set to true when the callback function is disabled, the shutdown_func function will print something. this item can be encapsulated into a class (without using global variables ).
 
PHP provides the register_shutdown_function () function, which can call back the registered function before the script ends, that is, the function executed after the PHP program is executed.
 
Php programmer site
 
Example:
 
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<Html xmlns = "http://www.w3.org/1999/xhtml">  
Register_shutdown_function is used to specify the functions that are executed after all scripts on this page are executed.
 
<? Php
 
Function aaa () {echo 'create file'; if ($ ttt = fopen ('d:/web_root/tx.txt ', "w +") // use an absolute path here, the relative path is invalid. For the reason, see the following explanation {fwrite ($ ttt, 'you are write after exit '); fclose ($ ttt) ;}} register_shutdown_function ('aaa '); // The function name can be enclosed in quotation marks without parentheses. The aa function is used when all statements on this page are executed successfully or time out. Exit ();?>
 
Register_shutdown_function execution mechanism is: php calls the function to the memory. Call this function when all php statements on the page are executed. Note: At this time, it is called from the memory instead of from the php page. Therefore, the relative path cannot be used in the above example, because php already does not exist. There is no relative path.

 

 

 


 
Note: register_shutdown_function is used to call a function after all php statements are executed. Do not call the function when the client closes the browser page.


 
The call conditions can be understood as follows:
 
1. When the page is forcibly stopped by the user
 
2. When the program code runs out of time
 
3. When PHP code execution is complete
From smile every day

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.