Home > Article > Backend Development > In-depth understanding of the difference between ob_flush and flush (how to use ob_flush() and flush())_PHP tutorial
About how to use ob_flush() and flush() in php
Note: The two functions ob_flush() and flush() are generally used together. The order is ob_flush() first, then flush(). Their function is to refresh the buffer.
Here is a detailed explanation of when to use the refresh buffer and why to refresh the buffer.
1. When to refresh the buffer
When the two functions file_get_contents() and file_put_contens() are used in the program, or when the program performs similar "read and write" functions or performs output operations to the browser, ob_flush() and flush() will be used. ) to flush the buffer.
2. Why refresh the buffer
Use file_get_contents() and file_put_content() as examples to explain.
The two functions file_get_contents() and file_put_conents() perform reading and writing data operations respectively. The data is first read into the memory and then written into the file, because the reading speed is faster than writing. The speed must be fast, so when your data is read, it does not mean that the data has been written. At this time, the more read content will be temporarily placed in the buffer (memory). It needs to be emphasized here that in fact, data reading Fetching and writing are two very fast actions.
Also to use an explanation (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 before sending the results to the browser until until the program ends. If you don't want the program to be output to the browser only after it is executed, then you can also use ob_flush() and flush() to refresh the cache.
In fact, flush() has another use, which is to output before the program ends. That is, part of the results can be output to the browser before a loop ends. This effect is very similar to the asynchronous transmission effect of ajax.
In-depth understanding of the difference between ob_flush and flush
The description of ob_flush/flush in the manual both refreshes the output buffer and needs to be used in conjunction, so it will cause Many people are confused...
In fact, they operate on different objects. In some cases, flush does nothing at all..
ob_* series of functions operate the output buffer of PHP itself. .
So, ob_flush refreshes PHP's own buffer.
And flush, strictly speaking, this can only be done when PHP is installed as an Apache Module (handler or filter). It has a practical effect. It refreshes the buffer of the WebServer (which can be considered to refer specifically to apache).
Under the sapi of the apache module, flush will indirectly call the api of apache by calling the flush member function pointer of sapi_module: ap_rflush refreshes the output buffer of apache. Of course, the manual also says that there are some other modules of apache that may change the result of this action.
Some Apache modules, such as mod_gzip, may perform output caching by themselves. , which will cause the results produced by the flush() function to not be sent to the client browser immediately.
Even the browser will cache the received content before displaying it. For example, the Netscape browser caches content until it receives a newline or the beginning of an html tag, and does not display the entire table until it receives a tag.
Some versions of Microsoft Internet Explorer will only start displaying the page after receiving 256 bytes, so some extra spaces must be sent to allow these browsers to display the page content. Therefore, use both correctly The order is. First ob_flush, then flush,
Of course, under other sapi, you can not call flush, but to ensure the portability of your code, it is recommended to use it together.