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 </table> 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.
Buffer----Flush ()
Buffer is a memory address space, and the Linux system default size is typically 4096 (1KB), which is a memory page. It is primarily used to store data between devices that are out of sync or between devices with different priority levels. By using buffer, you can make the process less of a mutual wait. Here is a popular example, when you open a text editor to edit a file, when you enter a character, the operating system does not immediately write this character directly to the disk, but the first write to buffer, when the buffer is filled with a buffer, the data will be written to disk, Of course, when the kernel function flush () is called, it is mandatory to write the dirty data in buffer back to disk.
Similarly, when executing echo,print, the output does not immediately pass TCP to the client browser display, but instead writes the data to PHP buffer. The PHP output_buffering mechanism means that a new queue is established before the TCP buffer, and the data must pass through the queue. When a PHP buffer is full, the script process will send the output data from PHP buffer to the system kernel to be displayed by TCP to the browser. So, the data will be written to these places echo/pring. PHP buffer, TCP buffer, browser
php output_buffering---ob_flush () /strong>
By default, PHP buffer is turned on, and the default value of this buffer is 4096, which is 1kb. You can find the output_buffering configuration in the php.ini configuration file. The output data is written to the PHP output_buffering when the user data is Echo,print, until Output_ Buffering is full, this data will be transmitted via TCP to the browser display. You can also manually activate the PHP output_buffering mechanism through Ob_start (), so that even if the output exceeds 1KB data, and does not really give the data to TCP to the browser, because ob_ Start () sets the PHP buffer space to a large enough . Data is sent to the client browser only until the script finishes, or when the Ob_end_flush function is called.
The use of these two functions is even the most puzzling problem of many people, the manual on the interpretation of two functions are also vague, do not explicitly point out their differences, it seems that both of the function is to refresh the output cache. But in the code at the beginning of our article, if Fush () is replaced with Ob_flush (), the program will no longer execute correctly. Obviously, they are different, or else the manual directly indicates that one of the other functions is an alias, there is no need to explain separately. So what is the difference between them?
When the cache is not turned on, the contents of the script output are in the state of waiting for output on the server side, and flush () can send the content waiting for output to the client immediately.
When the cache is turned on, the contents of the script output are stored in the output cache , and there is no content waiting for the output state, and you directly use flush () without sending anything to the client. The function of Ob_flush () is to remove the contents of the output cache, set to wait for the output state, but not directly to the client , you need to use Ob_flush () and then flush () before the client can immediately get the output of the script.
the correct order of flush and ob_flush should be, first ob_flush and then flush, as follows:
Ob_flush ();
Flush ();
If the Web server's operating system is a Windows system, then the order is reversed or not using Ob_flush () does not occur. [Pending VerificationHowever, the output buffer cannot be refreshed on a Linux system.
Output buffering function
1.bool Ob_start ([Callback $output _callback [, int $chunk _size [, BOOL $erase]])
Activates the output_buffering mechanism. Once activated, the script output is no longer directed to the browser, but is temporarily written to the PHP buffer memory area.
PHP opens the output_buffering mechanism by default, except that by calling the Ob_start () function The output_buffering value is extended to large enough. You can also specify $chunk_size to specify the value of Output_buffering. The default value of $chunk _size is 0, which means that data in PHP buffer is not sent to the browser until the end of the script run. If you set the size of the $chunk_size, the data in buffer is sent to the browser as long as the data length in the buffer reaches that value.
Of course, you can work with the data in buffer by specifying $ouput_callback. For example, function Ob_gzhandler, compress the data in buffer and then pass it to the browser.
The third parameter: whether to erase the cache, optional, the default is true, and if set to False, the cache will not be purged until the script execution is complete.
2.ob_get_contents
Get a copy of the data in PHP buffer. It is important to note that you should call the function before the Ob_end_clean () function call, otherwise ob_get_contents () returns an empty character.
You can use Ob_get_contents () to obtain server-side cached data as a string.
Using Ob_end_flush () outputs the cached data and closes the cache.
Using Ob_end_clean () silently clears the data cached by the server, without any data or other behavior.
The server-side cache is stacked, which means you can also open another cache Ob_start () within the Ob_start () after you turn it off.
but you also have to make sure that the cache is turned off as much as the number of open cache operations.
Ob_start () can specify a callback function to handle the cached data, if one ob_start () is nested inside another ob_start (), we assume that the outer ob_start (), the number is a, the inner layer of the Ob_start () number is B, Each of them developed a callback function of Functiona and FUNCTIONB respectively, then in cache b data output, it will be the predecessor FUNCITONB callback function processing, and then to the outer Functiona callback function processing, before output to the client.
In addition, the manual said that for some Web servers, such as Apache, in the use of callback functions may change the current working directory of the program, the solution is to manually modify the working directory in the callback function back, with the ChDir function, this seems not often encountered, when encountered remember to check the manual it.
3.ob_end_flush and Ob_end_clean
These two functions are a bit similar and will close the ouptu_buffering mechanism. But the difference is that Ob_end_flush simply flushes the data in PHP buffer (flush/send) to the client browser, and Ob_clean_clean empties the data in PHP bufeer (erase), but does not send it to the client browser.
before the Ob_end_flush call, the data in PHP buffer still exists, and ob_get_contents () can still get copies of the data in PHP buffer.
after the Ob_end_flush () call , Ob_get_contents () takes an empty string, and the browser receives no output, that is, no output.
You can use Ob_get_contents () to obtain data from the server-side cache as a string, using Ob_end_flush () to output cached data and close the cache.
Using Ob_end_clean () silently clears the data cached by the server, without any data or other behavior.