Home >Backend Development >PHP Tutorial >flushes PHP line-by-line output (combination of ob_flush and flush)

flushes PHP line-by-line output (combination of ob_flush and flush)

WBOY
WBOYOriginal
2016-07-29 08:47:551001browse

The description of ob_flush/flush in the manual is that they both refresh the output buffer, and they need to be used in conjunction, so it will cause confusion to many people...
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 is only available when PHP is used as an Apache Module (handler or filter) It only has practical effect when installed. It refreshes the buffer of WebServer (which can be considered to refer specifically to apache).
Under the sapi of 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 flush The results produced by the () function are not immediately sent to the client browser.
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 an 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.
So, the correct order to use the two 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 them together.
In IE , 256 bytes must be output first before it will work, the following code:

Copy code The code is as follows:


function execte(){
echo str_pad(" ", 256);
for ($i =1;$i<10;$i++){
echo $i."
";
ob_flush();
flush();
sleep(1);
}
}

The above introduces the flushes PHP line-by-line output (the combination of ob_flush and flush), including the content of flushes. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn