Difference between PHP flush () and ob_flush ()

Source: Internet
Author: User
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 devices with Different Storage speeds or with different priorities.
The area where data is transmitted between devices. 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 will not immediately write this character to the disk, but first write it to the buffer. When the buffer is full, the data in the buffer will be written to the disk.
When calling the kernel function flush (), you must write the dirty data in the buffer 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. PHP
The output_buffering mechanism means that a new queue is established before the TCP buffer, and data must pass through the queue. When a PHP
When the buffer is full, the script process will
The output data in the buffer is sent to the system kernel and sent to the browser for display by TCP. 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 find the output_buffering configuration in the PHP. ini configuration file.
When Echo, print, and other user data are output, the output data is written to PhP.
In output_buffering, when output_buffering is full, the data is transmitted to the browser over TCP. You can also
Ob_start () manually activate PHP
Output_buffering mechanism, so that even if the output exceeds 1 kb of data, it does not really send the data to TCP to the browser, because ob_start () will
Buffer space is set 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. But in our
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, it is also stated in the manual that one of them is another.
The alias of a function is enough. 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.

Open
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. While
Ob_flush () is used 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 need to use
After ob_flush () is used again, the client can 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
] In Linux, the output buffer cannot be refreshed.

Output buffering Function

1. bool
Ob_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 enough.
Large
. You can also specify $ chunk_size to specify the value of output_buffering. $ Chunk_size: The default value is 0, indicating that the script is running until the end of the script.
Buffer data will be sent to the browser. If you set the size of $ chunk_size
, Indicates that as long as the data length in the buffer reaches this value
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 server cache is stacked, that is to say, after you enable ob_start () and disable it
You 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 ()
You can specify a callback function to process cached data. If one ob_start () is nested with another ob_start (), we assume that the outer ob_start () is numbered.
Is a, the inner ob_start () number is B, and each of them has a callback function that is functiona and functionb, so when the data output in cache B, it
Will be the first funcitonb callback function processing, and then handed over to the outer functiona callback function processing, and then 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 applies PHP
Buffer (flush/send) to the client browser, while ob_clean_clean
The data in the bufeer is cleared (erase), but is not sent to the client browser.

Ob_end_flush before calling
, PHP
The data in the buffer still exists, and ob_get_contents () can still obtain PHP
The data in the 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 ()
You can specify a callback function to process cached data. If one ob_start () is nested with another ob_start (), we assume that the outer ob_start () is numbered.
Is a, the inner ob_start () number is B, and each of them has a callback function that is functiona and functionb, so when the data output in cache B, it
Will be the first funcitonb callback function processing, and then handed over to the outer functiona callback function processing, and then 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.