Analysis of output buffering _php techniques in PHP

Source: Internet
Author: User
Tags flush mixed
Let's look at a piece of code first:

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

OK, let's change this code and change it 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 just 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, 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 output is not as expected because the output_buffering buffers the output. The output will not be sent out until the 4096bytes is reached or the script is finished.

The function of Ob_end_clean () and Ob_end_flush () in the second 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 must set the output_buffering in php.ini 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.

Perhaps you will ask, since Ob_flush () is to send the buffered data out, then why 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 not flush () here is 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);
}
?〉
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 attach a very interesting code, the author is 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 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.