Table of Contents
register_shutdown_function
fastcgi_finish_request
Home Backend Development PHP Tutorial register_shutdown_function AND fastcgi_finish_request

register_shutdown_function AND fastcgi_finish_request

Jul 29, 2016 am 09:04 AM
error function request shutdown

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 1:

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";
Copy after login

The output information is as follows:

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) }

As you can see, the above The code catches out-of-memory errors 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>;
Copy after login

The page output result is: fail

It is visible, 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 scenario:

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","测试数据");
Copy after login

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

The above introduces register_shutdown_function AND fastcgi_finish_request, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Solution to PHP Fatal error: Call to undefined method PDO::prepare() in Solution to PHP Fatal error: Call to undefined method PDO::prepare() in Jun 22, 2023 pm 06:40 PM

PHP is a popular web development language that has been used for a long time. The PDO (PHP Data Object) class integrated in PHP is a common way for us to interact with the database during the development of web applications. However, a problem that some PHP developers often encounter is that when using the PDO class to interact with the database, they receive an error like this: PHPFatalerror:CalltoundefinedmethodPDO::prep

Can the linux shutdown command shut down immediately? Can the linux shutdown command shut down immediately? Jan 28, 2023 pm 05:26 PM

The Linux shutdown command shutdown can shut down the computer immediately. The root user only needs to execute the "shutdown -h now" command. The shutdown command can be used to perform the shutdown process and send messages to all programs being executed by users before shutting down. The shutdown command requires the system administrator root user to use it.

What should I do if 'Uncaught (in promise) Error: Request failed with status code 500' occurs when using axios in a Vue application? What should I do if 'Uncaught (in promise) Error: Request failed with status code 500' occurs when using axios in a Vue application? Jun 24, 2023 pm 05:33 PM

It is very common to use axios in Vue applications. axios is a Promise-based HTTP client that can be used in browsers and Node.js. During the development process, the error message "Uncaught(inpromise)Error: Requestfailedwithstatuscode500" sometimes appears. For developers, this error message may be difficult to understand and solve. This article will explore this

Solve the problem of 'error: incomplete type is not allowed' in C++ code Solve the problem of 'error: incomplete type is not allowed' in C++ code Aug 26, 2023 pm 08:54 PM

Solve the "error:incompletetypeisnotallowed" problem in C++ code. During the C++ programming process, you sometimes encounter some compilation errors. One of the common errors is "error:incompletetypeisnotallowed". This error is usually caused by operating on an incomplete type. This article will explain the cause of this error and provide several solutions. firstly, I

0271: What should I do if the computer cannot be turned on due to real time clock error? 0271: What should I do if the computer cannot be turned on due to real time clock error? Mar 13, 2023 am 11:30 AM

Solution to "0271: real time clock error" that cannot boot: 1. Press F1, and in the interface that appears, move the option bar to the third item "Date/Time"; 2. Manually change the system time to the current one time; 3. Press F10 and select yes in the pop-up dialog box; 4. Re-open the notebook to boot normally.

Solve the 'error: expected initializer before 'datatype'' problem in C++ code Solve the 'error: expected initializer before 'datatype'' problem in C++ code Aug 25, 2023 pm 01:24 PM

Solve the "error:expectedinitializerbefore'datatype'" problem in C++ code. In C++ programming, sometimes we encounter some compilation errors when writing code. One of the common errors is "error:expectedinitializerbefore'datatype'". This error usually occurs in a variable declaration or function definition and may cause the program to fail to compile correctly or

How to solve PHP Warning: fopen(): failed to open stream: No such file or directory How to solve PHP Warning: fopen(): failed to open stream: No such file or directory Aug 19, 2023 am 10:44 AM

How to solve PHPWarning:fopen():failedtoopenstream:Nosuchfileordirectory In the process of using PHP development, we often encounter some file operation problems, one of which is "PHPWarning:fopen():failedtoopenstream:Nosuchfileordirectory"

What does function mean? What does function mean? Aug 04, 2023 am 10:33 AM

Function means function. It is a reusable code block with specific functions. It is one of the basic components of a program. It can accept input parameters, perform specific operations, and return results. Its purpose is to encapsulate a reusable block of code. code to improve code reusability and maintainability.

See all articles