Brief introduction to the differences between ob_flush and flush in php

Source: Internet
Author: User
This article compares and analyzes ob_flush and flush in php to find out the differences between them. Ob_flush/flush descriptions in the manual are used to refresh the output buffer and must be used together, which may lead to many confusion...

In fact, they have different operation objects. in some cases, flush does nothing at all ..

Ob _ * series functions are used to operate the output buffer of PHP itself.

Therefore, ob_flush is used to refresh the PHP buffer.

But flush, strictly speaking, this is only useful when PHP is installed as an apache Module (handler or filter. it is used to refresh the buffer zone of the WebServer (which can be considered to be specific to apache.

In the sapi of apache module, flush indirectly calls apache api: ap_rflush to refresh apache output buffer by calling the flush member function pointer of sapi_module, some other modules of apache may change the result of this action ..

Some Apache modules, such as mod_gzip, May output the cache by themselves, which will cause the results produced by the flush () function to be not immediately sent to the client browser. Even the browser caches the received content before it is displayed. For example, the Netscape browser caches content before receiving a line break or the beginning of an html tag, and receivesThe entire table is not displayed until it is marked.

In some versions of Microsoft Internet Explorer, the page is displayed only after 256 bytes are received. Therefore, some extra spaces must be sent to display the page content in these browsers. Therefore, the correct sequence of use is. first, ob_flush and then flush. of course, in other SAPIs, you can do this without calling flush. However, we recommend that you use it to ensure the portability of your code.

The use of flush and ob_flush has some special attention, resulting in the failure to refresh the output buffer.

1. the correct sequence 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 windows, the order is reversed or the ob_flush () is not used. However, in Linux, the output buffer cannot be refreshed.

2. before using ob_flush (), make sure that the preceding content is 4069 characters long enough..

The output_buffering of some Web servers is 4069 characters or larger by default, that is, the output content must reach 4069 characters before the server will flush refresh the output buffer. to ensure that flush is effective, it is best to set the value in ob_flush () the following statements are available before the function:
Print str_repeat ("", 4096 );
To ensure that the value of output_buffering 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.