Understand the difference between ob_flush and flush. of ob_flush of and flush of

  • 2020-05-30 19:42:07
  • OfStack

Use of ob_flush() and flush() for php

Note: the functions ob_flush() and flush() are normally used in the order ob_flush() and flush (), which are used to flush the buffer.
Here are the details of when to flush the buffer and why to flush the buffer.

1. When should the buffer be flushed

When the file_get_contents() and file_put_contens() functions are used in the program, or when the program performs a similar "read and write" function or performs an output operation to the browser, ob_flush() and flush () are used to flush the buffer.

2. Why flush the buffer

Use file_get_contents() and file_put_content() as examples.

file_get_contents file_put_conents and () () these two functions respectively carry out the data read and write data manipulation, data is to be read into memory and then in written to the file, because read faster than the speed of writing, so when your data is read not represent data also finished writing, this time to read the content will be temporarily on the buffer (memory), l need to emphasize here, actually data reading and writing are two very fast action.

In another interpretation (when the program performs an output operation to the browser), individual web server programs, especially web server programs under Win32, will still cache the output of the script until the program ends before sending the result to the browser. If you don't want the program to finish executing before it outputs to the reader, you can also use ob_flush() and flush() to refresh the cache.

In fact, another use of flush () is to output before the end of the program, that is, to output part of the results to the browser before the end of a loop, which is similar to the asynchronous transfer effect of ajax.

Understand the difference between ob_flush and flush

ob_flush/flush are described in the manual as refreshing the output buffer and need to be used together, so it will cause a lot of confusion...

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

The ob_* series of functions is the output buffer for the operation PHP itself.

So, ob_flush is the buffer that refreshes PHP itself.

While flush, strictly speaking, is only useful if PHP is installed as Module(handler or filter) of apache, it is the buffer that refreshes WebServer(apache).

Under apache module's sapi, flush will indirectly refresh apache's output buffer by calling sapi_module's flush member function pointer, apache api: ap_rflush. Of course, as mentioned in the manual, there are some other apache modules that may change the result of this action..

Some Apache modules, such as mod_gzip, may do their own output caching, which results in the flush() function not being immediately sent to the client browser.

Even the browser caches the received content before it is displayed. For example, the Netscape browser caches the content before it receives a newline or the beginning of an html tag, and then receives it < /table > The entire table is not displayed before the tag.

Some versions of Microsoft Internet Explorer don't display the page until after 256 bytes have been received, so you have to send 1 extra space for these browsers to display the content of the page. So, the correct order for both is ob_flush, then flush,

Of course, it's ok not to call flush under other sapi, but to ensure the portability of your code, it's recommended to use it as a companion.

Related articles: