A deep understanding _php technique based on PHP output caching (output_buffering)

Source: Internet
Author: User
Tags sapi

First, make a clear order of PHP output
1. PHP output cache turned on:Echo,print-> PHP output_buffring-> server buffering-> browser buffering-> browser display
2. PHP output Cache not open:Echo,print-> Server buffering-> browser buffering-> browser display

Also specify the browser's output cache: IE for 256Bytes, Chrome and Firefox for 1000Bytes, only output data reached this length or script end browser will output data on the page

here are a few of the PHP settings and APIs that you use:
Output_buffering Configuration in 1.php.ini
off: means to turn off the PHP output cache
on: Open an infinitely large output cache
• 4096: Open output Cache size 4096Byte

Implicit_flush Configuration in 2.php.ini
on: Indicates that the flush () function is automatically invoked after each output (such as Echo,print), directly outputting
off: In contrast to ON, the flush () is not invoked after each output, and it needs to wait until the server buffering is full, but we can replace it with the flush () function, and it doesn't matter if it's not open, but more flexible.

3.ob_flush () function: Remove the data from the PHP buffering and put it in the server buffering

4.flush () Function: Remove the server buffering data and put the browser buffering

5.ob_start () function:I don't know much about this function now, because the output will not be ob_flush (), even with Ob_flush () and flush (), and the data cannot be immediately exported to the browser. Now I know if output_buffering= Off, even if the use of Ob_start (), is not able to cache output data, and if output_buffering=on, even if not ob_start (), output data can also be PHP cache, so feel ob_start more waste, temporarily regardless of his

Then let's take a look at the code (set Output_buffering=4096,implicit_flush=off)

Copy Code code as follows:

<body>
<?php
Ob_start (); This thing will not be normal when it is opened, the output is not controlled by Ob_flush (), I don't know what to do with
Echo str_repeat (', 1000); IE Cache 256Bytes
Echo str_repeat (', 1000); Chrome and FF cache 1000Bytes, which is used to first cache the browser, but it is very doubtful why this line of output is not saved by output_buffering, but directly output
For ($i =0 $i <5; $i + +) {
echo $i. ' <br/> ';
Ob_flush ();
Flush ();
Sleep (1);
}
?>
</body>

Here the output of the code is one line output, the specific principle of the ob_flush () and flush () function of the effect
These two functions are missing any one in my setup is to wait until 0, 1, 2, 3, 4 are all cached together after the last output
Finally quoted a section of Laruence blog, I hope to help you understand

Ob_flush/flush in the manual description, are refreshing output buffer, and also need to support the use, so will lead to a lot of people confused ...

In fact, they are different objects of operation, in some cases, flush do not do anything at all.

The Ob_* series function is the output buffer that operates on PHP itself.

So, Ob_flush is the buffer that refreshes PHP itself.

And flush, strictly speaking, this only in PHP as the Apache module (handler or filter) installed when the actual role. It is a buffer that refreshes the webserver (which can be considered specifically Apache).

Under the SAPI of Apache module, flush invokes the Flush member function pointer of the sapi_module and indirectly invokes Apache's Api:ap_rflush to refresh the Apache output buffer, which is also stated in the manual. There are some other Apache modules that may change the results of this action.
1. Some Apache modules, such as Mod_gzip, may have their own output caching,
2. This will cause the results of the flush () function to not be immediately sent to the client browser.
3.
4. Even browsers will cache incoming content before it is displayed. such as Netscape
5. The browser caches the content before it accepts the start of a newline or HTML tag, and
6. The entire table will not be displayed until the </table> mark is accepted.
7.
8. Some versions of Microsoft Internet Explorer only receive 256
9. Bytes will not start displaying the page, so you must send some extra space to make this
10. Some browsers display page content.
So, the order in which the two are used correctly is. First Ob_flush, then flush,
Of course, in other sapi, do not call the flush can also, just to ensure that your code portability, recommended supporting use.

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.