PHP Buffer Usage Summary, PHP buffer usage _php Tutorial

Source: Internet
Author: User
Tags php error

PHP Buffer Usage Summary, PHP buffer usage


This example summarizes the PHP buffer usage. Share to everyone for your reference, as follows:

Let's look at a piece of code first.

<?phpfor ($i =10; $i >0; $i-) {  echo $i;  Flush ();  Sleep (1);}? >

As the PHP manual says:

This 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

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

<?phpfor ($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?

<?phpfor ($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)

<?phpob_implicit_flush (True); for ($i =10; $i >0; $i-) {  echo $i;  Ob_flush (); #如果ob函数打开的情况下  Sleep (1);}? >
<?phpob_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 Puttyshell. 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.

<?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";? >

Read more about PHP related content readers can view the topic: "PHP Basic Grammar Introductory Tutorial", "PHP error and Exception handling method summary" and "PHP common functions and Skills summary"

I hope this article is helpful to you in PHP programming.

Articles you may be interested in:

    • Refresh the PHP buffer for your site acceleration
    • Code for Buffer control implementation in PHP
    • PHP output Buffer control output control series functions detailed
    • PHP Nested output Buffer Code instance
    • PHP Buffered Output Example analysis
    • Ob_get_length buffering and retrieving buffer length instances in PHP
    • PHP buffering output_buffering and Ob_start usage Introduction
    • PHP buffer output_buffering Use of detailed
    • PHP ob_flush,flush in IE buffer Invalid workaround
    • PHP Flush class Output Buffer profiling
    • Php-accelerator website Accelerated php buffering method

http://www.bkjia.com/PHPjc/1099061.html www.bkjia.com true http://www.bkjia.com/PHPjc/1099061.html techarticle PHP Buffer Usage Summary, PHP buffer Usage This example summarizes the PHP buffer usage. Share to everyone for your reference, as follows: Let's look at a piece of code first. Phpfor ($i =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.