Usage and difference of flush () and ob_flush () and ob_end_flush () in php _ PHP Tutorial

Source: Internet
Author: User
The usage and difference between flush () and ob_flush () and ob_end_flush () in php. This article will give you a summary of the usage and differences between flush (), ob_flush (), and ob_end_flush () in php. For more information, see the reference. Buffer is a summary of the usage and difference of flush () and ob_flush () and ob_end_flush () in php, for more information, see references.

Buffer, which is a memory address space of 4096 (1 kb) [in php. output_buffering configuration is found in the ini configuration file. php has the php output_buffering mechanism. When php code is executed, the content is not output immediately, instead, the echo/print content is output to the buffer. when the buffer is full, the data is handed over to the system kernel and sent to the browser for display. when the php output_buffering mechanism is enabled (enabled by default, you can enable it through the ob_start () function. only when the data in the php buffer reaches the set value will the data in the buffer be sent to the browser. However, browsers also have caches. some versions of browser output content only when the data reaches 256 bytes,
Ob_start () function: Open the output buffer.
Function format void ob_start (void)
Note: When the buffer zone is activated, all non-file header information from the PHP program is not sent, but stored in the internal buffer zone. To output the buffer content, you can use ob_end_flush () or flush () to output the buffer content.

Flush: refresh the buffer content and output it.
Function format: flush ()
Note: This function is frequently used and highly efficient.

Ob_get_contents: returns the content of the internal buffer.
Function format: string ob_get_contents (void)
Note: This function returns the content in the current buffer. if the output buffer is not activated, FALSE is returned.

Ob_get_length: returns the length of the internal buffer.
Function format: int ob_get_length (void)
Note: This function returns the length of the current buffer. it is the same as ob_get_contents. if the output buffer is not activated, FALSE is returned.

Ob_end_clean: delete the content of the internal buffer and disable the internal buffer.
Function format: void ob_end_clean (void)
Note: This function will not output the content of the internal buffer but delete it.

Ob_end_flush: sends the content of the internal buffer to the browser and closes the output buffer.
Function format: void ob_end_flush (void)
Description: This function sends the content of the output buffer (if any)

Ob_implicit_flush: Enables or disables absolute refresh.
Function format: void ob_implicit_flush ([int flag])
Note: the buffer zone is disabled by default. After absolute output is enabled, the output of each script is directly sent to the browser, and you do not need to call flush ()


Flush () can immediately send the content waiting for output to the client, while ob_flush () can only output the content when the buffer is full. you can verify it using the following simple php instance:

Instance

The code is as follows:

// This prevents browser caching
Echo str_repeat ("", 1024 );
For ($ I = 0; $ I <5; $ I ++ ){
Echo $ I;
Sleep (1 );
Flush (); // A number is output every 1 s, but the use of ob_flush () will wait for 5s to output together.
}
?>

Usage and difference between lift () and ob_flush () and ob_end_flush (). If you need to know more, refer. First, let's talk about buffer, which is...

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.