Flush () and ob in PHP

Source: Internet
Author: User
Buffer ---- flush () buffer is a memory address space. The default size of Linux is generally 4096 (1 kb), that is, a memory page. It is mainly used for data transmission between devices with Different Storage speeds or devices with different priorities. Through the buffer, the mutual waits of processes can be reduced. Here is a simple example.

Buffer ---- flush () buffer is a memory address space. The default size of Linux is generally 4096 (1 kb), that is, a memory page. It is mainly used for data transmission between devices with Different Storage speeds or devices with different priorities. Through the buffer, the mutual waits of processes can be reduced. Here is a simple example.

Buffer ---- flush ()

Buffer is a memory address space. The default size of a Linux system is generally 4096 (1 kb), that is, a memory page. It is mainly used for data transmission between devices with Different Storage speeds or devices with different priorities. Through the buffer, the mutual waits of processes can be reduced. Here is a simple example. When you open a text editor and edit a file, the operating system does not immediately write this character to the disk for each input, instead, it is written to the buffer first. When the buffer is full, the data in the buffer is written to the disk. Of course, when the kernel function flush () is called, the dirty data in the buffer must be written back to the disk.

In the same way, when echo and print are executed, the output is not immediately transmitted to the client browser through tcp, but is written to the php buffer. The php output_buffering mechanism means that a new queue is established before the tcp buffer, and data must pass through the queue. When a php buffer is full, the script process sends the output data in the php buffer to the system kernel and sends it to the browser for display. Therefore, the data will be written to these places in sequence echo/pring-> php buffer-> tcp buffer-> browser

Php output_buffering --- ob_flush ()

By default, php buffer is enabled, and the default value of this buffer is 4096, that is, 1 kb. You can use. in the ini configuration file, find output_buffering configuration. when echo, print, and other user data are output, the output data will be written to php output_buffering until output_buffering is fully written. The data will be transmitted to the browser over tcp for display. You can also manually activate php output_buffering through ob_start (), so that even if the output exceeds 1 kb, the data is not really sent to the browser over tcp, because ob_start () set the php buffer space to large enough. Data is sent to the client browser only after the script is completed or the ob_end_flush function is called.

The use of these two functions is perhaps the most confusing problem for many people. The interpretation of the two functions in the manual is also not detailed, and the difference between them is not clearly pointed out, it seems that both functions are to refresh the output cache. However, in the code at the beginning of this article, if fush () is replaced with ob_flush (), the program cannot be correctly executed. Obviously, they are different. Otherwise, you can directly describe one of them as the alias of another function in the manual. It is not necessary to describe them separately. So what are their differences?

When the cache is not enabled, the content output by the script is waiting for output on the server side. flush () can immediately send the content waiting for output to the client side.


After the cache is enabled, the content output by the script is stored in the output cache, and there is no content waiting for the output. You can directly use flush () without sending any content to the client. The function of ob_flush () is to extract the content that already exists in the output cache and set it to wait for the output status, but it will not be directly sent to the client. In this case, you need to first use ob_flush () then use flush () to obtain the Script output immediately.

1. the correct order of flush and ob_flush should be: First ob_flush and then flush, as shown below:
Ob_flush ();
Flush ();
If the operating system of the Web server is windows, the order is reversed or the ob_flush () is not used. [To be verified] the output buffer cannot be refreshed on Linux.

Output buffering Function

1. boolOb_start([Callback $ output_callback [, int $ chunk_size [, bool $ erase])
Activate the output_buffering mechanism. Once activated, the Script output is not directly sent to the browser, but is temporarily written to the php buffer memory area.

By default, php enables the output_buffering mechanism. However, by calling the ob_start () function, the value of output_buffering is extended to a large enough value. You can also specify $ chunk_size to specify the value of output_buffering. $ Chunk_size: The default value is 0, indicating that data in php buffer will not be sent to the browser until the script stops running. If you set the size of $ chunk_size, it indicates that as long as the data length in the buffer reaches this value, the data in the buffer will be sent to the browser.

Of course, you can specify $ ouput_callback to process data in the buffer. For example, the ob_gzhandler function compresses the data in the buffer and then transmits it to the browser.

The third parameter: whether to erase the cache. Optional. The default value is true. If it is set to false, the cache will not be cleared until the script is executed.

2. ob_get_contents
Obtain a copy of data in the php buffer. It is worth noting that you should call this function before calling the ob_end_clean () function; otherwise, ob_get_contents () returns an empty character.

You can use ob_get_contents () to obtain data cached by the server in the string format,

When ob_end_flush () is used, the cached data is output and the cache is disabled.
The use of ob_end_clean () will silently clear the data cached by the server without any data or other behavior.


The cache on the server is stacked. That is to say, you can enable another cache ob_start () in the server before enabling ob_start ().

However, you must ensure that the number of operations to disable the cache is the same as that to enable the cache.
Ob_start () can specify A callback function to process cached data. If an ob_start () is nested with another ob_start (), we assume that the outer ob_start () is, the inner ob_start () number is B, and each of them develops a callback function which is functionA and functionB, so when the data output in cache B, it will be processed by the funcitonB callback function, it is then handed to the functionA callback function of the outer layer for processing before it can be output to the client.

In addition, the manual says that for some web servers, such as apache, using the callback function may change the current working directory of the program, the solution is to manually modify the working directory in the callback function and use the chdir function. This is not uncommon. Remember to check the manual when encountering this problem.

3. ob_end_flush and ob_end_clean
These two functions are similar, and the ouptu_buffering mechanism is disabled. But the difference is that ob_end_flush only flush the data in the php buffer to the client browser, while ob_clean_clean clears the data in the php bufeer (erase), but does not send it to the client browser.

Before calling ob_end_flush, data in php buffer still exists, and ob_get_contents () can still obtain data copies in php buffer.

After ob_end_flush () is called, ob_get_contents () gets an empty string, and the browser cannot receive the output, that is, no output.

You can use ob_get_contents () to obtain the cached data on the server in the string format. Using ob_end_flush (), the cached data is output and the cache is disabled.
The use of ob_end_clean () will silently clear the data cached by the server without any data or other behavior.
The cache on the server is stacked. That is to say, you can enable another cache ob_start () in the server before enabling ob_start (). However, you must ensure that the number of operations to disable the cache is the same as that to enable the cache.
Ob_start () can specify A callback function to process cached data. If an ob_start () is nested with another ob_start (), we assume that the outer ob_start () is, the inner ob_start () number is B, and each of them develops a callback function which is functionA and functionB, so when the data output in cache B, it will be processed by the funcitonB callback function, it is then handed to the functionA callback function of the outer layer for processing before it can be output to the client.

In addition, the manual says that for some web servers, such as apache, using the callback function may change the current working directory of the program, the solution is to manually modify the working directory in the callback function and use the chdir function. This is not uncommon. Remember to check the manual when encountering this problem.

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.