In-depth understanding of PHP output cache (output_buffering) _ PHP Tutorial

Source: Internet
Author: User
Based on the in-depth understanding of PHP output cache (output_buffering. First clarify the PHP output sequence 1. open the php output cache: echo, print-phpoutput_buffring-serverbuffering-browserbuffering-browserdisplay2. not open the php output cache First, clarify the PHP output sequence
1. open the php output cache: echo, print-> php output_buffring-> server buffering-> browser display
2. php output cache not enabled: echo, print-> server buffering-> browser display

In addition, it is clear that the browser's output cache: IE is 256 Bytes, Chrome and FireFox are 1000 Bytes. the browser will output the data on the page only when the output data reaches this length or the script ends.

Let's talk about several PHP settings and APIs used:
1. output_buffering configuration in php. ini
• Off: disables the PHP output cache.
• On: open an infinitely large output cache
• 4096: Open the output cache with a size of 40 96 bytes

2. implicit_flush configuration in php. ini
• On: indicates that after the flush () function is automatically called after each output (such as echo and print), the output is directly
• Off: in contrast to On, flush () is not called after each output. it is output only when the server buffering is full, but we can replace it with the flush () function, it does not matter if it is not enabled, but it is more flexible.

3. ob_flush () function: extracts data from PHP buffering and places the data in server buffering.

4. flush () function: extracts Server buffering data and places it in browser buffering.

5. ob_start () function:I do not know much about this function, because after it is enabled, the output will not be controlled by ob_flush (), even if ob_flush () and flush () are used (), the data cannot be output on the browser immediately. now, if output_buffering = Off, even if ob_start () is used, the output data cannot be cached. if output_buffering = On, even if ob_start () is not used (), the output data can also be cached by PHP, so it is considered that ob_start is obsolete.

Let's take a look at the code (set output_buffering = 4096, implicit_flush = Off)

The code is as follows:




// Ob_start (); // The output is not controlled by ob_flush (). I don't know what to use.
// Echo str_repeat ('', 1000); // 256 Bytes cached by IE
Echo str_repeat ('', 1000); // Chrome and FF cache 1000 Bytes, which is used to cache the browser first, but I am wondering why the output of this row is not stored by output_buffering, but directly output
For ($ I = 0; $ I <5; $ I ++ ){
Echo $ I .'
';
Ob_flush ();
Flush ();
Sleep (1 );
}
?>



Here, the code output result is output in one row. for details, refer to the functions of the ob_flush () and flush () functions.
If either of these functions is missing, it will wait until 0, 1, 2, 3, and 4 are cached and output together.
Finally, I will reference a piece of Laruence blog, hoping to help you understand it.

Ob_flush/flush descriptions in the manual are used to refresh the output buffer and must be used together, which may lead to many confusion...

In fact, they have different operation objects. in some cases, flush does nothing at all ..

Ob _ * series functions are used to operate the output buffer of PHP itself.

Therefore, ob_flush is used to refresh the PHP buffer.

But flush, strictly speaking, this is only useful when PHP is installed as an apache Module (handler or filter. it is used to refresh the buffer zone of the WebServer (which can be considered to be specific to apache.

In the sapi of apache module, flush indirectly calls apache api: ap_rflush to refresh apache output buffer by calling the flush member function pointer of sapi_module, some other modules of apache may change the result of this action ..
1. some Apache modules, such as mod_gzip, may cache output by themselves,
2. this will cause the results generated by the flush () function to not be immediately sent to the client browser.
3.
4. even the browser caches the received content before it is displayed. For example, Netscape
5. the browser caches content before receiving a line break or the beginning of the html tag, and
6. acceptThe entire table is not displayed until it is marked.
7.
8. some versions of Microsoft Internet Explorer only receive 256
9. the page is displayed only after the byte, so some extra spaces must be sent for this
10. some browsers display the page content.
Therefore, the correct order of the two users is: first ob_flush, then flush,
Of course, other SAPIs do not call flush, but we recommend that you use it to ensure the portability of your code.

Protocol 1. php output cache is enabled: echo, print-php output_buffring-server buffering-browser display 2. php output cache is not enabled...

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.