In depth understanding of PHP output_buffering based on PHP output cache

  • 2020-06-12 08:42:18
  • OfStack

First, specify the output order of PHP in 1
1. php output cache: echo,print - enabled > php output_buffring - > server buffering - > browser buffering - > browser display
php output cache: echo,print - not enabled > server buffering - > browser buffering - > browser display

In addition, specify the output cache of the browser :IE is 256Bytes, Chrome and FireFox are 1000Bytes. The browser will output the data on the page only when the output data reaches this length or the script ends

A few more PHP Settings and API used:
1. output_buffering configuration in ES36en. ini
The & # 8226; Off: Means to turn off the PHP output cache
The & # 8226; On: Turns on the infinite output cache
The & # 8226; 4096: Turns on the output cache of size 4096Byte

implicit_flush configuration in ES49en. ini
The & # 8226; On: represents the direct output after the flush() function is automatically called after each output (e.g. echo,print)
The & # 8226; Off: In contrast to On, flush() is not called after each output until server buffering is full, but we can replace it with the flush() function, which is more flexible if not turned on

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

4.flush() function: take out the data of Server buffering and put it into browser buffering

5.ob_start() function: I don't know much about this function at the moment, because the output is not controlled by ob_flush(), and even if ob_flush() and flush() are used, the data cannot be immediately output to the browser. It is now known that if output_buffering=Off, even if ob_start() is used, the output data cannot be cached if output_buffering=On , the output data can be cached by PHP even if ob_start() is not used, so ob_start is considered invalid, leaving it alone

Then let's look at the code (set output_buffering=4096,implicit_flush=Off)


<html>
     <body>
         <?php
             // ob_start();    // This thing doesn't work properly when it's on , The output is not affected by ob_flush() control , I don't know what it's for 
             // echo str_repeat(' ' ,1000);    //IE The cache 256Bytes
             echo str_repeat(' ' ,1000);    //Chrome and FF The cache 1000Bytes, I'm going to use the browser cache first , But it's very confusing 1 Why is the line output not taken output_buffering save , I'm just going to print it 
             for($i=0;$i<5;$i++) {
                 echo $i.'<br />';
                 ob_flush();
                 flush();
                 sleep(1);
             }
         ?>
     </body>
 </html>

The output of this code is line 1, line 1, line 1. For details, please refer to the ob_flush() and flush() functions under 1
Either one of these functions is missing and in my case I'm going to have to wait until the 0's, 1's, 2's, 3's and 4's are all cached and then the last one comes out
Finally, I quote paragraph 1 of Laruence blog, hoping to help you understand

ob_flush/flush described in the manual is to flush the output buffer, and it needs to be used together, so it will lead to a lot of confusion...

In fact, they operate on different objects. In some cases, flush doesn't do anything at all.

ob_* series functions are output buffers that operate on PHP itself.

So, ob_flush is the buffer to flush PHP itself.

flush, strictly speaking, is only useful when PHP is installed as Module(handler or filter) of apache. It is a buffer to refresh WebServer.

Under apache module's sapi, flush will indirectly call api: ap_rflush to refresh the output buffer of apache by calling the sapi_module member function pointer. Of course, as mentioned in the manual, there are some other MODULES of apache, which may change the result of this action.
1. Some Apache modules, such as mod_gzip, may cache the output by themselves.
2. This will cause the results from the flush() function not to be immediately sent to the client browser.
3.
Even the browser caches what it receives before displaying it. For example Netscape
5. The browser caches the content before it receives a newline or the beginning of the html tag, and at
6. Received < /table > The entire table is not displayed before the tag.
7.
8.1 Some versions of Microsoft Internet Explorer are only accepted when 256
9. The page isn't displayed until bytes later, so 1 extra space must be sent for this
Some browsers display page content.
So, the order to use them correctly is ob_flush, then flush,
Of course, under other sapi, it's possible not to call flush, but for the portability of your code, it's recommended.


Related articles: