Simple talk about the difference _php skills of ob_flush and flush in PHP

Source: Internet
Author: User
Tags flush sapi

Ob_flush/flush in the manual description, are refreshing output buffer, and also need to support the use, so will lead to a lot of people confused ...

In fact, they are different objects of operation, in some cases, flush do not do anything 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 in PHP as the Apache module (handler or filter) installed when the actual role. 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 the sapi_module and indirectly invokes 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 will result in the results of the flush () function not being immediately sent to the client browser. Even browsers will cache what they receive 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 will 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 in which the two are used correctly is. First Ob_flush, then flush, of course, under other sapi, do not call flush can also, but in order to ensure that your code portability, recommended supporting use.

The use of flush and Ob_flush has some special attention in place, resulting in the inability to flush output buffers.

I. Flush and ob_flush in the correct order, first Ob_flush flush, as follows:
Ob_flush ();
Flush ();
If the Web server's operating system is a Windows system, the order is reversed or the Ob_flush () does not appear to be problematic. However, the output buffer cannot be refreshed on the Linux system.

two. Before using Ob_flush (), make sure that the previous content size is enough 4069 characters .

Some Web server output_buffering default is 4069 characters or greater, that is, the output must reach 4069 characters server will flush refresh output buffer, in order to ensure that flush valid, preferably in the Ob_flush () function before the following statement:
Print Str_repeat ("", 4096);
To ensure that the output_buffering value is reached.

Copy Code code as follows:

<?php
For ($i =1 $i <20; $i + +)
{
echo "<font size= ' color= ' Red ' >". $i. " </font> ";
Echo ' <br> ';
Ob_flush ();
Flush ();
Sleep (1);
}
Ob_end_flush ();
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.