Home >Backend Development >PHP Tutorial >register_shutdown_function AND fastcgi_finish_request,registershutdownhook_PHP教程

register_shutdown_function AND fastcgi_finish_request,registershutdownhook_PHP教程

WBOY
WBOYOriginal
2016-07-12 08:59:36954browse

register_shutdown_function AND fastcgi_finish_request, registershutdownhook

In php, the two methods are executed when the request is about to end. The method names are register_shutdown_function and fastcgi_finish_request respectively. Although the timing of execution is similar, the functions and application scenarios are different. Comparing the differences between the two methods is not the focus of this article. The focus of this article is to explain the application scenarios of the two methods.

register_shutdown_function

Function:

Register a method. When a request request is executed, the registered method is called. Note that even if an error occurs during execution and this request is forced to exit, the registered method will still be executed.

Application scenario one:

You can use its features to capture some detailed information of some errors. The sample code is as follows:

<span>function</span><span> catch_error(){
</span><span>$error</span> =<span> error_get_last();
</span><span>if</span>(<span>$error</span><span>){
</span><span>var_dump</span>(<span>$error</span><span>);
}
}
</span><span>register_shutdown_function</span>("catch_error"<span>);
</span><span>ini_set</span>('memory_limit','1M'<span>);
</span><span>$content</span> = <span>str_repeat</span>("aaaaaaaaaaaaaaaaaaaaaaa",100000<span>);
</span><span>echo</span> "aa";

The output information is roughly as follows:

<p>array(4) { ["type"]=> int(1) ["message"]=> string(80) "Allowed memory size of 1048576 bytes exhausted (tried to allocate 2300001 bytes)" ["file"]=> string(39) "/test.php" ["line"]=> int(13) }</p>

It can be seen that the above code captures the out of memory error normally.

Application Scenario 2
Check whether the request is closed normally. The sample code is as follows:

<span>function</span><span> monitor(){
</span><span>global</span> <span>$is_end</span><span>;
</span><span>if</span>(<span>$is_end</span> == <span>true</span><span>){
</span><span>echo</span> "success"<span>;
}</span><span>else</span><span>{
</span><span>echo</span> "fail"<span>;
}
}
</span><span>register_shutdown_function</span>("monitor"<span>);
</span><span>$is_end</span> = <span>false</span><span>;
</span><span>die</span><span>();
</span><span>$is_end</span> = <span>true</span>;

The output result of the page is: fail

It can be seen that even if the die function is called. The registered monitor function also executes normally.

fastcgi_finish_request

Function:
flush data to the client. After calling this method, any output content will not be output to the client.

Application scenarios:

If part of the processing content of a request does not need to be sent to the client, you can first generate the content output to the client, and then call this method. After the method is called, the content will be output to the client. Content that does not need to be output to the client can be placed after this method. This improves responsiveness. The sample code is as follows:

<span>echo</span> "a"<span>;
fastcgi_finish_request();
</span><span>echo</span> "b"<span>;
</span><span>file_put_contents</span>("/tmp/test","abc.com"<span>);
</span><span>die</span><span>();
</span><span>file_put_contents</span>("/tmp/test2","测试数据");

The page output result is: a
It can be seen that the echo "b" after the fastcgi_finish_request method is not output to the client. But you will find that files are created normally in the /tmp/test directory. But the /tmp/bo56 file was not created

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1097792.htmlTechArticleregister_shutdown_function AND fastcgi_finish_request, registershutdownhook In php, the two methods are executed when the request is about to end. The method names are register_shutdown_...
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