Php_ output Cache (output_buffering) for an in-depth understanding of edge execution side output

Source: Internet
Author: User
Tags sapi browser cache

first clarify the output order of PHP
1. PHP output Cache is turned on:   echo,print, PHP output_buffring, server buffering, browser Buffering-browser display
2. The PHP output cache is not open:   Echo, Browser display

, browser buffering, print, server buffering

Another explicit browser output cache: IE 256Bytes, Chrome and Firefox 1000Bytes, Only the output data reaches this length or the script end browser will output the data on the page

to say a few more PHP settings and APIs:
1.php.ini in the output_buffering configuration
? Off: Indicates that the PHP output cache is closed
? On: Open an infinitely large output cache
? 4096: Open output cache with size 4096Byte

2.php.ini in Implicit_flush configuration
? On: Indicates that the flush () function is automatically called after each output (such as Echo,print), and the
is output directly? OFF: Contrary to On, flush () will not be called after each output, it will need to wait until the server buffering full to output, but we can use the flush () function instead of it, not open is OK, but more flexible

3.ob_flush () function: Take out the data in PHP buffering and put it into the server buffering

4.flush () function: Remove the data from the server buffering and put it into browser buffering

5.ob_start () function:for this function I do not understand very clearly, because the output will not be controlled by Ob_flush (), even if the use of Ob_flush () and flush (), the data will not be immediately output in the browser. Now know that if output_buffering= OFF, even if the use of Ob_start (), is also unable to cache the output data, and if output_buffering=on, even without ob_start (), output data can also be PHP cache, so feel ob_start compared to waste, temporarily regardless of his

Then we'll see.CodeBar (set Output_buffering=4096,implicit_flush=off)

Copy Code The code is as follows:

<body>
<?php
//Ob_start ();//This thing will be abnormal when it is opened, output is not controlled by Ob_flush (), do not know what to do with
//Echo Str_repeat (', 1000); IE cache 256Bytes
Echo str_repeat (","),//chrome and FF Cache 1000Bytes, which is used to first use the browser cache, but it is doubtful why this line of output is not output_ The buffering is saved, but it is directly output
for ($i =0; $i <5; $i + +) {
echo $i. ' <br/> ';
Ob_flush ();
Flush ();
Sleep (1);
}
;
</body>

Here the code output is a row of output, the specific principle of the Ob_flush () and the Flush () function of the functions of the
These two functions are missing any one in my setting is to wait until 0, 1, 2, 3, 4 are all cached and last together output
last reference a section of Laruence blog, I hope that everyone's understanding is helpful

ob_flush/flush in the manual description, all is to refresh the output buffer, and also need to use the companion, so it will cause a lot of people confused ...

Actually, the two of them have different objects, and in some cases, flush doesn't do anything at all:

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

So, Ob_flush is the buffer that refreshes PHP itself.

Flush, strictly speaking, this only works when PHP is installed as an Apache module (handler or filter). It is a buffer that refreshes the webserver (which can be considered specifically Apache).

Under Apache module's SAPI, flush will invoke Sapi_module's flush member function pointer indirectly, calling Apache's Api:ap_rflush to flush Apache's output buffer, and of course the manual says, There are some Apache other modules that may change the result of this action:
1. Some Apache modules, such as Mod_gzip, may have their own output cache,
2. This causes the flush () function to produce results that are not immediately sent to the client browser.
3.
4. Even the browser will cache the received 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 is not displayed until the </table> tag is accepted.
7.
8. Some versions of Microsoft Internet Explorer only receive 256
9. The byte does not start to display this page, so you must send some extra space to make this
10. Some browsers display the contents of the page.
Therefore, the correct use of the two is the order. Ob_flush first, then flush,
Of course, in other sapi, do not call flush also can, only to ensure that your code portability, recommended for use.

Php_ output Cache (output_buffering) for an in-depth understanding of edge execution side output

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.