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 in turn to these places Echo/print, PHP buffer---browser
php output_buffering---ob_flus H ()
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, it does not really give the data to TCP to the browser, because Ob_start () set 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 verification] but the output buffer cannot be refreshed on the 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.
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.
The most detailed PHP flush () and OB