Analyze PHP output buffer flush and so on

Source: Internet
Author: User

<? PHP
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

<? PHP
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.

<? PHP
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 ??

<? PHP
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)

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

<? PHP
Ob_end_clean ();
Ob_implicit_flush (true );
For ($ I = 10; $ I> 0; $ I --)
{
Echo $ I;
Sleep (1 );
}
?>

Please note that the above ob_implicit_flush (true) function forces the output to be immediately sent to the browser whenever there is an output. In this way, you do not need to use flush () to send the output (ECHO) to the browser.

The above complaints may not be true in some browsers. Because browsers also have their own rules. I used firefox1.5, IE6, opera8.5 for testing. Opera cannot be output normally, because it has a rule that no HTML Tag is encountered, and it will never be output unless the script ends. Firefox and IE are normal.

Finally, I attached a piece of very interesting code, prepared by puttyshell. In a script cycle, each output overwrites the previous output.
The following code is only available in Firefox. other browsers do not support multipart/X-mixed-replace Content-Type.

<? PHP
Header ('content-type: multipart/X-mixed-replace; boundary = endofsection ');
Print "\ n -- endofsection \ n ";

$ PMT = array ("-", "\", "| ","/");
For ($ I = 0; $ I <10; $ I ++ ){
Sleep (1 );
Print "Content-Type: text/plain \ n ";
Print "part $ I \ t". $ PMT [$ I % 4];
Print "-- endofsection \ n ";
Ob_flush ();
Flush ();
}
Print "Content-Type: text/plain \ n ";
Print "the end \ n ";
Print "-- endofsection -- \ n ";
?>

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.