Php script stop callback function

Source: Internet
Author: User
Php scripts often die when terminating callback function scripts, which are not always so nice. we do not want to show users a fatal error or a blank page (when display_errors is set to off ). which of the following statements is in PHP? Register_shutdown_function. let's set a php script to terminate the callback function.

Scripts often die and are not always so nice-looking. we don't want to show users a fatal error or a blank page (when display_errors is set to off). Is there a name in PHP? The register_shutdown_function function allows us to 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.

$ Clean? =? False ;?
Function? Shutdown_func (){?
Global? $ Clean ;?
If? (! $ Clean ){?
Die ("not? A? Clean? Shutdown ");?
}?
Return? False ;?
}?

Register_shutdown_function ("shutdown_func ");?

$? =? 1 ;?
$? =? 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:
?
?
?
?
Register_shutdown_function example?
?
?
$ Starttime = microtime (true );?
Www.phperz.com?
Function? Test (){?
$ Starttime = microtime (true );????
If (! File_exists('Test.txt ')){????? // Determine if the object does not exist !!?
$ Str? =? Fopen('Test.txt ', "w + ");????????
Fwrite ($ Str, 'I wrote it at the end. Time: $ starttime ');?
Fclose ($ Str );?
Echo? "Created! Creation time: $ starttime ";?
}?
Else? {? // If yes ;?
Echo? 'File already exists ';?
}?
}?
Register_shutdown_function ('test ');?
Echo? "Program start:". $ starttime ."
";? Phperz ~ Com?

For ($ I = 0; I I <1000; $ I ++ ){?
Echo? "Echo
";?
}?
Exit ;?
?>?
?

?

?

Register_shutdown_function is used to specify the functions that are executed after all scripts on this page are executed.
Function? Aaa ()? {?
Echo? 'Create file ';?
If ($ ttt? =? Fopen ('d:/web_root/tx.txt ', "w +") // use an absolute path. relative paths are invalid. For the reason, please refer to the explanation below?
{?
Fwrite ($ ttt, 'you? Are? Write? After? Exit ');?
Fclose ($ ttt );?
}?
}?

Register_shutdown_function ('aaa ');//? The function name does not need to be enclosed in brackets and can be enclosed in quotation marks. When all the statements on this page are executed completely, or the aa function times 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

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.