How to use Ob_flush () and flush () for PHP
Note: The functions of Ob_flush () and flush () are commonly used together, in the Order of first Ob_flush (), and then Flush (), which is the function of refreshing the buffer.
Here specifically, when to use the refresh buffer and why to flush the buffer.
When to flush a buffer
Ob_flush () and flush () are used to flush the buffer when the program uses either the File_get_contents () and File_put_contens () functions, or if the program performs a similar read-write function or performs an output operation to the browser.
Second, why to refresh the buffer
Use File_get_contents () and File_put_content () as an example to explain.
The two functions, file_get_contents () and File_put_conents (), perform both read and write data operations, which are read in memory and then written to the file because the speed of reading is faster than that of writing. So when you read the data does not mean that the data is also written, this time the content will be temporarily put into the buffer (memory), where you need to emphasize, in fact, data reading and writing is two very fast action oh.
Also with an explanation (when the program performs an output operation to the browser), the individual Web server program, especially the Web server program under Win32, will still cache the output of the script until the program is finished before sending the results to the browser. You can also use Ob_flush () and flush () to flush the cache if you do not want the program to finish before it outputs to the device.
In fact, flush () has a purpose, is to output before the end of the program, that is, a loop is not finished can output some of the results to the browser, this effect is very similar to the asynchronous transfer effect of Ajax. The
deep understanding of the differences between Ob_flush and flush
Ob_flush/flush described in the manual is refreshing the output buffer and needs to be matched, so many people are confused ...
In fact, they have different objects of action, and in some cases flush do nothing 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 works when PHP is installed as a module (handler or filter) for Apache. 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 Sapi_module, indirectly invoking 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.
Some Apache modules, such as Mod_gzip, may have their own output caching, which causes the results of the flush () function not to be immediately sent to the client browser.
Even browsers will cache what is received before it is displayed. For example, Netscape browser caches content before it accepts the start of a newline or HTML tag, and does not display the entire table until the </table> tag is accepted.
Some versions of Microsoft Internet Explorer only start displaying the page after the 256 bytes received, so you must send some extra spaces to allow the browsers to display the page content so the order to use them correctly is. First Ob_ Flush, and then flush,
Of course, under other Sapi, it's OK not to call flush, just to keep your code portable, it's recommended for use.