Details of the differences between PHP flush of and ob_flush of

  • 2020-06-03 06:12:45
  • OfStack

buffer ---- flush()

buffer is a memory address space, and the default size of Linux system is usually 4096(1kb), i.e. one memory page. An area used to store data between devices with asynchronous speeds or different priorities. With buffer, processes can be made to wait for each other less. Here said a popular example of 1 point, you open a text editor to edit a file, every time you enter a character, the operating system does not immediately put the characters written to the disk directly, but to write to buffer, when with a buffer will put buffer write magnetic disk, the data of course when the kernel function flush (), forced to write the dirty data in buffer back to disk.
Similarly, when echo,print is executed, the output is not immediately displayed via tcp to the client browser, but written to php buffer. php output_buffering mechanism means that before tcp buffer, a new queue is created through which data must pass. When 1 php buffer is full, the script process will send the output data from php buffer to the system kernel for tcp to the browser for display. So, the data will be written to these places in turn echo/pring - > php buffer - > tcp buffer - > browser

php output_buffering --- ob_flush()

By default, php buffer is on, and the default value for buffer is 4096, or 1kb. You can find the output_buffering configuration in the ES45en.ini configuration file. When echo,print, etc output user data, the output data will be written to php output_buffering, until output_buffering is full, the data will be sent to the browser through tcp display. You can also manually activate the php output_buffering mechanism via ob_start(), so that even if the output exceeds 1kb, you don't really give the data to tcp to the browser, because ob_start() has set the php buffer space to large enough. Data is not sent to the client browser until the end of the script, or until the ob_end_flush function is called.

The use of these two functions may be the most confusing problem for many people. The explanation of the two functions in the manual is vague, and the difference between them is not clearly indicated. It seems that the function of both functions is to refresh the output cache. But if you replace fush() with ob_flush() in the code at the beginning of our article 1, the program will no longer execute correctly. Obviously, they are different, otherwise the manual would simply state that one of them is an alias for the other function, so there is no need to specify them separately. So what's the difference?

When caching is not enabled, the output of the script is in a waiting state on the server side, and flush() can immediately send the waiting output to the client.

When caching is enabled, the output of the script is stored in the output cache. There is nothing waiting for output, and you use flush() directly without sending anything to the client. The function of ob_flush() is to pull out the contents of the output cache and set it to wait for output, but not send it directly to the client. In this case, you need to use ob_flush() and then flush() for the client to get the output of the script immediately.

1. The correct order of flush and ob_flush should be ob_flush and then flush, as follows:
ob_flush();
flush();
If the Web server is operating on an windows system, then reversing the order or not using ob_flush() will not be a problem. It is not possible to refresh the output buffer on Linux.

output buffering function
1.bool ob_start ([ callback $output_callback [, int $chunk_size [, bool $erase ]]] )
Activate the output_buffering mechanism. Once activated, script output is no longer directly sent to the browser, but temporarily written to the php buffer memory area.
php turns on the output_buffering mechanism by default, except that by calling the ob_start() function the output_buffering value is extended sufficiently. 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 from php buffer will not be sent to the browser until the script has finished running. If you set the size of $chunk_size, the data in buffer will be sent to the browser as soon as the data length in buffer reaches this value.
Of course, you can process the data in buffer by specifying $ouput_callback. The ob_gzhandler function, for example, compresses the data in buffer and sends it to the browser.
Parameter 3: Cache erases, optional, default is true, if set to false, the cache will not be cleared until the end of script execution.
2.ob_get_contents
Obtain 1 copy of data from php buffer. Note that you should call ob_end_clean() before the ob_end_clean() function call, otherwise ob_get_contents() returns 1 null character.

You can use ob_get_contents() to get the data cached by the server as a string,
Using ob_end_flush() outputs the cached data and closes the cache.
Using ob_end_clean() silently clears data cached by the server without any data or other behavior.
The cache on the server side is stacked, meaning that you can turn on ob_start() internally before turning it off.

But you also want to make sure that you have one more operation to turn off the cache than you have to turn on the cache.
ob_start() can specify one callback function to handle cached data. If one ob_start() is internally nested with another ob_start(), we assume that the outer ob_start() is numbered A and the inner ob_start() is numbered B, and they each specify one callback function, functionA and functionB, respectively. When caching the data output in B, it will be processed by the funcitonB callback and passed to the outer functionA callback before being output to the client.

In addition, the manual states 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 change the working directory in the callback function.

3. ob_end_flush ob_end_clean
These two functions are similar in that they both turn off the ouptu_buffering mechanism. The difference, however, is that ob_end_flush simply flusher the data from php buffer (flush/send) to the client browser, while ob_clean_clean flusher the data from php bufeer (erase) but does not send it to the client browser.

Before ob_end_flush is called, the data in php buffer still exists and ob_get_contents() can still get a copy of the data in php buffer.

After the call to ob_end_flush(), ob_get_contents() gets an empty string, and the browser receives no output, that is, no output.

You can use ob_get_contents() to get the data cached by the server as a string, and ob_end_flush() to output the cached data and close the cache.
Using ob_end_clean() silently clears data cached by the server without any data or other behavior.
The cache on the server side is stacked, meaning that you can turn on ob_start() internally before turning it off. But you also want to make sure that you have one more operation to turn off the cache than you have to turn on the cache.
ob_start() can specify one callback function to handle cached data. If one ob_start() is nested inside another ob_start(), we assume that the outer ob_start() is numbered A and the inner ob_start() is numbered B, and they specify one callback function respectively, functionA and functionB. When caching the data output in B, it will be processed by the previous funcitonB callback function, and then passed to the outer functionA callback function, before it can be output to the client.

In addition, for some web servers, such as apache, it is possible to change the current working directory of the program by using the callback function. The solution is to manually change the working directory in the callback function.


Related articles: