Analysis of PHP output buffer flush-PHP source code

Source: Internet
Author: User
Ec (2); let's look at a piece of code first. & Lt ;? Phpfor ($ i10; $ I & gt; 0; $ I --) {& nbsp; echo $ I; & nbsp; flush (); & nbsp; sleep (1 );}? & Gt; according to the statement in the php manual & nbsp; this function sends all the output of the program so far to script ec (2); script

Let's take a look at a piece of code.

For ($ I = 10; $ I> 0; $ I --)
{
Echo $ I;
Flush ();
Sleep (1 );
}
?>

According to the php Manual

This function sends all the output of the program so far to the user's browser.

The above code should output $ I every second. But not necessarily in reality. It may be that after 10 seconds, all the output will be displayed at the same time.

Okay. Let's change this code

Ob_end_clean (); // modify part
For ($ I = 10; $ I> 0; $ I --)
{
Echo $ I;
Flush ();
Sleep (1 );
}
?>

Hey, after adding this sentence ob_end_clean ();, it's OK. In fact, we can change ob_end_clean () to ob_end_flush.

Let me change it.

For ($ I = 10; $ I> 0; $ I --)
{
Echo $ I;
Ob_flush (); // modify part
Flush ();
Sleep (1 );
}
?>

Run the command to check whether $ I is output every second? Why?
Don't worry. Let's take a look at php. ini.

Open php. ini and search for output_buffering. We will see settings such as output_buffering = 4096. Like output_buffering, this setting is used to buffer the output and the buffer size is 4096bytes.

In our first piece of code, the output was not as expected because output_buffering buffered the output. Output will not be sent if it does not reach 4096bytes or the script ends.

The role of ob_end_clean () and ob_end_flush () in the second code is to terminate the buffer. In this way, it will not be sent out until there is a buffer of 40 bytes.

In the third code, an ob_flush () is used to send the buffered data, but the buffer is not terminated. Therefore, it must be flush () at each time () before use.

If you do not want to use ob_end_clean (), ob_end_flush (), and ob_flush (), you must set output_buffering in php. ini to be small enough, for example, to 0. It should be noted that if you plan to use ini_set ("output_buffering", "0") in the script, please stop. This method is not feasible. Because at the beginning of the script, the buffer settings are loaded, and then the buffer starts.

Maybe you will ask, since ob_flush () sends the buffered data, why do we still need to use flush ()??? Can't I use the following code directly ??

For ($ I = 10; $ I> 0; $ I --)
{
Echo $ I;
Ob_flush ();
Sleep (1 );
}
?>

Note the differences between ob_flush () and flush. The former is to release data from the buffer of PHP, and the latter is to send data not in the buffer or released to the browser. So when the buffer exists, we must use both ob_flush () and flush.

Is flush () indispensable here? No, we have another way to make it immediately sent to the browser when there is data output. The following two sections of Code do not need to use flush. (When you set output_buffering to 0, you do not need ob_flush () or ob_end_clean)

Ob_implicit_flush (true );
For ($ I = 10; $ I> 0; $ I --)
{
Echo $ I;
Ob_flush ();
Sleep (1 );
}

<

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.