Home >Backend Development >PHP Tutorial >Detailed explanation of PHP register_shutdown_function function_PHP tutorial

Detailed explanation of PHP register_shutdown_function function_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-13 17:49:43859browse

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 us to set another function that can be called when the execution is shut down. 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. Therefore, 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 along the way. I've prepared a very basic example that demonstrates when a fatal error needs to be displayed, How should you give the user some appropriate feedback. You can make the example look better by turning off the display of fatal errors (Annotation: you can set display_errors and error_reporting).


$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 clean variable is not set to true when the shutdown callback function is running, 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 executed.

php programmer station

Example:



register_shutdown_function example "; phperz~com for($i=0;$i<1000;$i++){ echo "Echo
"; } exit; ?>

The function of register_shutdown_function is to specify the function to be executed after all scripts on this page are executed.


function aaa() { echo 'Create file'; if($ttt = fopen('D:/web_root/tx.txt',"w+")) //Absolute path must be used here, relative path will be invalid. Please see the explanation below for the reason { fwrite($ttt,'you are write after exit'); fclose($ttt); } } register_shutdown_function('aaa'); // The function name does not need to have parentheses, just use quotes. . The aa function is used when all statements on this page are executed or times out. exit(); ?>

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 are executed. Do not understand it as calling the function when the client closes the streaming browser page.



The calling condition can be understood like this:

1. When the page is forced to stop by the user

2. When the program code times out

3. When the PHP code execution is completed
Excerpted from Smile Every Day

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478332.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...
in PHP
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