Home  >  Article  >  php教程  >  Let’s briefly talk about the difference between ob_flush and flush in php

Let’s briefly talk about the difference between ob_flush and flush in php

高洛峰
高洛峰Original
2016-12-22 16:34:191043browse

ob_flush/flush described in the manual both flush the output buffer and need to be used together, so it will cause confusion to many people...

In fact, they operate on different objects. In some cases, flush does nothing at all. Things...

ob_* series 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) has an actual effect only when it is 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 flush member function pointer of sapi_module. Call apache's api: ap_rflush refreshes apache's output buffer. Of course, the manual also says that there are some other apache modules that may change the result of this action...

Some Apache modules, such as mod_gzip, may do it themselves Output caching, which will cause the results produced by the flush() function to not be sent to the client browser immediately. Even browsers cache received content before displaying it. For example, the Netscape browser caches content until it receives a line break or the beginning of an html tag, and does not display an entire table until it receives a f16b1740fad44fb09bfe928bcc527e08 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, the correct order to use the two is. First ob_flush, then flush. Of course, under other sapi, it is okay not to call flush. However, in order to ensure the portability of your code, it is recommended to use them together.

flush and ob_flush There are some special considerations in use that prevent the output buffer from being refreshed.

1. The correct order of flush and ob_flush, first ob_flush and then flush, as follows:
ob_flush();
flush();
If the operating system of the web server is a windows system, then reverse the order or not use ob_flush() No problem will arise. But on Linux systems, the output buffer cannot be flushed.

2. Before using ob_flush(), make sure the previous content size is enough to 4069 characters.

The default output_buffering of some web servers is 4069 characters or larger, that is, the output content must reach 4069 characters before the server flushes the output buffer. In order to ensure that the flush is effective, it is best to have the following statement before the ob_flush() function:
print str_repeat(" ", 4096);
to ensure the output_buffering value is reached.

<?php
for ($i=1; $i<20; $i++)
{
echo "<font size=&#39;10&#39; color=&#39;red&#39;>".$i."</font>";
echo &#39;<br>&#39;;
ob_flush();
flush();
sleep(1);
}
ob_end_flush();
?>

For more related articles about the difference between ob_flush and flush in PHP, please pay attention to PHP Chinese website!

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