PHP Script Stop callback function

Source: Internet
Author: User
Tags blank page
PHP Script termination callback function

Scripts often die, and are not always so good-looking. We don't want to show the user a fatal error, or a blank page (in case display_errors is set to OFF). Is there a name in PHP? The Register_shutdown_function function allows us to set up another function that can be called when execution is closed. That is, our function will be called when the execution of our script completes or the Accidental death causes PHP execution to close. So, We can use the method of setting a variable to false at the beginning of the script and then set it to true at the end of the script, so that the PHP shutdown callback function checks whether the script is complete or not. If our variable is still false, we know that the last line of the script has not been executed, so it must have been executed in the execution of the program. I have prepared a very basic example that can demonstrate the need to display a fatal error when How should you give the user some appropriate feedback? You can make the example look good by turning off the display of fatal errors (Display_errors and error_reporting can be set).

$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 because of fatal error
$clean?=?true;?

?>

As you can see, if the shutdown callback function is run, the clean variable is not set to the True,shutdown_func function and will print something. This thing could be packaged into a class (without using global variables).

?

?

?

PHP provides register_shutdown_function (), a function that can callback a registered function before the script terminates, that is, a function that executes when the PHP program executes.

PHP Programmer Station

Example:
?
?
?
?
<title>register_shutdown_function Example</title>?
?
?
$starttime = Microtime ( true );?
www . Phperz . com?
function? Test (){?
$starttime = Microtime ( true );????
if (! file_exists ( ' Test.txt ' )){????? //Judge if the file does not exist!!?
$Str? =? fopen ( ' Test.txt ' , "w+" );????????
fwrite ( $Str , ' I wrote it at the end. Time: $starttime ' );?
fclose ( $Str );?
Echo? "Create complete!" Creation time: $starttime " ;?
}?
Else? {? //if present;?
echo? ' file already exists ' ;?
}?
}?
register_shutdown_function ( ' Test ' );?
Echo? "program start:" . $starttime . "
" ;? Phperz ~ com?

For ( $i = 0 ; $i < + ; $i ++) {?
Echo? "Echo
" ;?
}?
Exit;?
?
?

?

?

The role of register_shutdown_function is to specify functions that are executed after all script execution on this page is completed.
function?AAA()? {?
Echo?' Create file ';?
if ($ttt?=?fopen(' D:/web_root/tx.txt ',"w+"))  the absolute path is used here, and the relative path is invalid. Why don't you see the explanation behind it?
{?
fwrite($TTT,' You?are?write?after?exit ');?
fclose($TTT);?
}?
}?

register_shutdown_function(' AAA ');  The function name can be enclosed in quotation marks without parentheses. When all statements on this page are executed, or when the AA function is timed out.
exit ();?
?>

The register_shutdown_function execution mechanism is: PHP calls the function to call into memory. Call this function when all the PHP statements on the page are completed. Note that the call from memory at this time is not called from the PHP page, so the above example cannot use a relative path, because PHP has already been used when the original page does not exist. There is no relative path to be made.

Note: Register_shutdown_function is the function that is called after all PHP statements have been executed and not understood to be called when the client closes the streaming browser page.
You can understand the invocation condition this way:
1. When the page is forcibly stopped by the user

2. When the program code runs over 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.