Deep understanding of the difference between Ob_flush and flush (Ob_flush () and flush () Usage _php tutorial

Source: Internet
Author: User
Tags sapi
How to use Ob_flush () and flush () for PHP

Note: the Ob_flush () and flush () functions are generally used together, in order to Ob_flush () first, then flush (), and their function is to flush the buffer.
Here's a specific next time to use the flush buffer and why to flush the buffer.

First, when to flush the buffer

Ob_flush () and flush () are used to flush buffers when the program uses both file_get_contents () and File_put_contens () functions, or when a similar read-write function is performed in the program or when output is performed to the browser.

Second, why to flush the buffer

Use File_get_contents () and file_put_content () as examples to explain.

The file_get_contents () and file_put_conents () functions perform both read and write data operations, where the data is read into memory and then written to the file, because the read speed is faster than the write, So when your data is read, it does not mean that the data is written, this time the content will be temporarily put into the buffer (memory), where it needs to be emphasized, in fact, the data read and write is two very fast action oh.

Also with an explanation (when the program performs output operations to the browser), individual Web server programs, especially Web server programs under Win32, still cache the output of the script until the end of the program, before sending the results to the browser. You can also use Ob_flush () and flush () to flush the cache if you do not want the program to complete before you print it out.

In fact, flush () also has a purpose, is to do not end the program before the output, that is, a loop is not finished to output some of the results to the browser, this effect is similar to Ajax asynchronous transmission effect.

Deep understanding of the difference between Ob_flush and flush

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 the 256 bytes are accepted, so you must send some extra spaces to let these browsers display the page content so the order in which they are used correctly is. First Ob_flush, then flush,

Of course, in other sapi, do not call flush also can, only to ensure that your code portability, recommended for use.

http://www.bkjia.com/PHPjc/326450.html www.bkjia.com true http://www.bkjia.com/PHPjc/326450.html techarticle for PHP Ob_flush () and flush () Use note: Ob_flush () and flush () are generally used together, the order is first Ob_flush (), and then flush (), their function is to refresh ...

  • 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.