Flush and ob in php

Source: Internet
Author: User
Differences between flush and ob_flush in php (2011-11-11:3) protocol phpflushob_flush differences Miscellaneous: PHPbuffer ---- flush () buffer is a memory address space, in Linux, the default size is generally 4096 (1 kb), that is, a memory page. It is mainly used for transmission between devices with Different Storage speeds or devices with different priorities.

Differences between flush and ob_flush in php (15:31:17) hybrid php flush ob_flush differences Miscellaneous: PHP buffer ---- flush () buffer is a memory address space, in Linux, the default size is generally 4096 (1 kb), that is, a memory page. It is mainly used for transmission between devices with Different Storage speeds or devices with different priorities.

Difference between flush and ob_flush in php (15:31:17)

Bytes

Phpflushob_flush differences Classification: PHP

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.

Similarly, when echo and print are executed, the output is not immediately transmitted to the client browser through tcp, but is written to phpbuffer. The php output_buffering mechanism means that a new queue is established before tcpbuffer, and data must pass through the queue. When a php buffer is full, the script process sends the output data in phpbuffer 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, phpbuffer 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 is written to phpoutput_buffering until output_buffering is fully written. The data is transmitted to the browser over tcp. You can also manually activate the phpoutput_buffering mechanism through ob_start (), so that even if the output exceeds 1 kb, the data cannot be sent to the browser over tcp because ob_start () set the phpbuffer 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 cache is not enabled, the content output by the script is waiting for output on the server., Flush () can immediately send the content waiting for output to the client.


After the cache is enabled, the content output by the script is stored in the output cache.At this time, there is no content waiting for 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 is not directly sent to the client.In this case, you must first use ob_flush () and then use flush () to obtain the Script output immediately.

I.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] In Linux, the output buffer cannot be refreshed.

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 phpbuffer will not be sent to the browser until the script stops running. If you set the size of $ chunk_size, 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 phpbuffer. 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 server cache is stacked, that is to say, after you enable ob_start () and disable itYou can enable another cache 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. However, the difference is that ob_end_flush only flush the data in phpbuffer to the client browser, while ob_clean_clean clears the data in phpbufeer (erase), but does not send it to the client browser.

Ob_end_flush before calling, The data in phpbuffer still exists, and ob_get_contents () can still obtain data copies in php buffer.

After ob_end_flush () is calledOb_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.

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.