PHP Flush class output buffering analysis _php skills

Source: Internet
Author: User
Tags mixed

<?php


for ($i =10; $i >0; $i-)


{


echo $i;


Flush ();


sleep (1);


}


?>


according to the PHP manual


This function sends all output from the current program to the user's browser.


The above code, should be output once every second $i. But this is not necessarily the case in practice. It is possible to wait for 10 seconds after all the output is presented at the same time.


Well, let's change this code to
.

<?php


Ob_end_clean ()/Modify part


for ($i =10; $i >0; $i-)


{


echo $i;


Flush ();


sleep (1);


}


?>


Hey, add this sentence Ob_end_clean (), incredibly ok. In fact, we change the Ob_end_clean () to Ob_end_flush () is also OK.


I'll change it again.


<?php


for ($i =10; $i >0; $i-)


{


echo $i;


Ob_flush ()/Modify part


Flush ();


sleep (1);


}


?>


run a bit, is not found $i also every second output once? What is this for?


don't worry, let's take a look at php.ini.


open php.ini, search output_buffering, and we'll see settings like this output_buffering = 4096. As its name output_buffering, the effect of this setting is to buffer the output, with a buffer size of 4096bytes.


in our first code, the reason we don't output as expected is because the output_buffering buffers the output. The output will not be sent out until the 4096bytes is reached or the script is finished.


and the role of Ob_end_clean () and Ob_end_flush () in the second paragraph of the code is to terminate the buffer. This doesn't have to wait until a 4096bytes buffer is sent out.


in the third code, a 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 have to set the php.ini in output_buffering small enough, for example, to 0. Note that if you're going to use Ini_set ("output_buffering", "0″") in your script, stop it, this method doesn't work. Because at the beginning of the script, the buffering settings are loaded, and the buffer begins.


you might ask, since Ob_flush () is sending the buffered data, why do you need to use flush ()??? Can't you just use the code below??


<?php


for ($i =10; $i >0; $i-)


{


echo $i;


Ob_flush ();


sleep (1);


}


?>


Please note the difference between ob_flush () and flush (). The former releases data from the PHP buffer, which sends the data that is not in the buffer or is released to the browser. So when buffering exists, we have to Ob_flush () and flush () at the same time.


is that the flush () here is the indispensable? No, we have another way to get it out to the browser right away when there's a data output. The following two pieces of code do not need to use flush (). (When you set the output_buffering to 0, Ob_flush () and Ob_end_clean () are not needed)


<?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 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 action may not be valid in some browsers. Because the browser also has its own rules. I tested it with firefox1.5,ie6,opera8.5. Opera can not output normal, because it has a rule, that is, do not encounter an HTML tag, it is absolutely not output, unless the script end. And Firefox and IE are relatively normal.


finally attached a very interesting code, the author of Puttyshell. In a script cycle, each output will overwrite the previous output.


The following code is available only under Firefox, and other browsers do not support Multipart/x-mixed-replace's 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\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.