Simply talk about the difference between Ob_flush and flush in PHP _php

Source: Internet
Author: User
Keywords Php ob_flush flush
Tags sapi
Ob_flush/flush's description in the manual, is to refresh the output buffer, and also need to be used, so it will cause many people confused ...

In fact, the two of them are different objects, in some cases, flush does not do anything at all.

The Ob_* series function is the output buffer that operates PHP itself.

So, Ob_flush is the buffer that refreshes PHP itself.

Flush, strictly speaking, this only works when PHP is installed as an Apache module (handler or filter). It is a buffer that refreshes the webserver (which can be considered specifically Apache).

Under Apache module's SAPI, flush will invoke Sapi_module's flush member function pointer indirectly, calling Apache's Api:ap_rflush to flush Apache's output buffer, and of course the manual says, There are some Apache other modules that may change the result of this action:

Some Apache modules, such as Mod_gzip, may make their own output caches, which will result in the flush () function producing results that are not immediately sent to the client browser. Even the browser will cache the received content before it is displayed. For example, the Netscape browser caches content before it accepts the start of a newline or HTML tag, and does not display the entire table until the tag is accepted.

Some versions of Microsoft Internet Explorer do not start to display the page until 256 bytes are accepted, so you must send some extra spaces to let these browsers display the page content. So, the correct use of the two is the order. First Ob_flush, then flush, of course, in other sapi, do not call flush can also, only to ensure that your code portability, recommended for use.

Flush and Ob_flush have some special attention on the use of the place, resulting in unable to refresh the output buffer.

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

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

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

The code is as follows:


<?php
for ($i =1; $i <20; $i + +)
{
echo "". $i. "";
Echo '
';
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.