PHP Buffer Summary

Source: Internet
Author: User
Category: PHP Time: December 29, 2014

In some loops execution of the statement, the browser does not have the expected output, exactly what is the problem? This article mainly explains the PHP buffer. For PHP, every operation like the echo output is written to PHP buffer, and the data is displayed on the browser when the script is executed or the force output cache is executed.

Let's look at a piece of code first.

for ($i =10; $i >0; $i-) {echo $i; flush (); sleep (1);}

According to the PHP manual, the function sends all the output of the program so far to the user's browser. The above code should output $i once every second. But this is not necessarily the case in practice. It is possible that after waiting for 10 seconds, all the output is presented at the same time.

OK, let's change this code to

Ob_end_clean ();//Modify part for ($i =10; $i >0; $i-) {echo $i; flush (); sleep (1);}

Hey, add this sentence Ob_end_clean (); it's OK. In fact, we can change Ob_end_clean () to Ob_end_flush () as well.

I'll change it again.

for ($i =10; $i >0; $i-) {echo $i; Ob_flush ();//Modify part flush (); sleep (1);}

Run, is it found that the $i also output once every second? What is this for? Don't worry, let's take a look at php.ini.

Open php.ini, search for output_buffering, and we'll see settings like this output_buffering = 4096. As its name output_buffering, this setting is the function of buffering the output, the buffer size is 4096bytes.

In our first piece of code, the reason for not outputting as expected is that the output_buffering buffers the output. The output will not be sent out until the 4096bytes is reached or the script is finished.

The effect of Ob_end_clean () and Ob_end_flush () in the second piece of code is to terminate the buffer. This will not have to wait until there is a 4096bytes buffer to be sent out.

In the third code, a sentence of Ob_flush () is used to send the buffered data, but it does not terminate the buffer, so it must be used before each flush ().

If you do not want to use Ob_end_clean (), Ob_end_flush () and Ob_flush (), we must set the output_buffering in php.ini to be small enough, for example, to set to 0. It is important to note that if you are going to use Ini_set ("output_buffering", "0″") in your script to set it up, please stop, this method is not possible. Because at the beginning of the script, the buffering settings are already loaded, and then the buffering begins.

You might ask, since Ob_flush () is sending the buffered data, why flush () is required??? Can't you just use the following code?

for ($i =10; $i >0; $i-) {echo $i; Ob_flush (); sleep (1);}

Please note the difference between ob_flush () and flush (). The former is the release of data from the buffer of PHP, the latter is not buffered or released data sent to the browser. So when the buffers are present, we have to use both ob_flush () and flush ().

Is that the flush () that is indispensable here? No, we have another way to get the data out and send it to the browser right away. The following two pieces of code do not need to use flush (). (When you set output_buffering to 0, even Ob_flush () and Ob_end_clean () are not required)

Ob_implicit_flush (TRUE); for ($i =10; $i >0; $i-) {echo $i; Ob_flush (); #如果ob函数打开的情况下sleep (1);}   Ob_end_clean (); Ob_implicit_flush (TRUE); for ($i =10; $i >0; $i-) {echo $i; sleep (1);}

Note the above Ob_implicit_flush (true), which forces the output to be sent to the browser whenever there is an output. This does not require each output (echo) to be sent to the browser with flush ().

The above may not be true in some browsers. Because the browser also has its own rules. I used firefox1.5,ie6,opera8.5 to test it. Where opera does not output normally, because it has a rule that does not encounter an HTML tag, it is absolutely not output, unless the script ends. And Firefox and IE are relatively normal.

Finally, I enclose a very interesting code, the author is the Putty shell. In a script cycle, each output will overwrite the previous output. The following code is only available under Firefox, and other browsers do not support Multipart/x-mixed-replace's content-type.

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\n"; print "part $i \ t". $pmt [$i% 4]; print "-- Endofsection\n "; Ob_flush (); Flush (); } print "content-type:text/plain\n\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.