Home > Article > Backend Development > Detailed explanation of the difference between PHP flush() and ob_flush()_PHP Tutorial
buffer ---- flush()
buffer是一个内存地址空间,Linux系统默认大小一般为4096(1kb),即一个内存页。主要用于存储速度不同步的设备或者优先级不同的 设备之间传办理数据的区域。通过buffer,可以使进程这间的相互等待变少。这里说一个通俗一点的例子,你打开文本编辑器编辑一个文件的时候,你每输入 一个字符,操作系统并不会立即把这个字符直接写入到磁盘,而是先写入到buffer,当写满了一个buffer的时候,才会把buffer中的数据写入磁 盘,当然当调用内核函数flush()的时候,强制要求把buffer中的脏数据写回磁盘。
同样的道理,当执行echo,print的时候,输出并没有立即通过tcp传给客户端浏览器显示, 而是将数据写入php buffer。php output_buffering机制,意味在tcp buffer之前,建立了一新的队列,数据必须经过该队列。当一个php buffer写满的时候,脚本进程会将php buffer中的输出数据交给系统内核交由tcp传给浏览器显示。所以,数据会依次写到这几个地方echo/pring -> php buffer -> tcp buffer -> browser
php output_buffering --- ob_flush()
默认情况下,php buffer是开启的,而且该buffer默认值是4096,即1kb。你可以通过在php.ini配置文件中找到output_buffering配置.当echo,print等输出用户数据的时候,输出数据都会写入到php output_buffering中,直到output_buffering写满,会将这些数据通过tcp传送给浏览器显示。你也可以通过 ob_start()手动激活php output_buffering机制,使得即便输出超过了1kb数据,也不真的把数据交给tcp传给浏览器,因为ob_start()将php buffer空间设置到了足够大 。只有直到脚本结束,或者调用ob_end_flush函数,才会把数据发送给客户端浏览器。
这两个函数的使用怕是很多人最迷惑的一个问题,手册上对两个函数的解释也语焉不详,没有明确的指出它们的区别,似乎二者的功能都是刷新输出缓存。但在我们文章一开始的代码中如果讲fush()替换成ob_flush(),程序就再不能正确执行了。显然,它们是有区别的,否则也手册中直接说明其中一个是另外一个函数的别名即可了,没必要分别说明。那么它们的区别到底是什么呢?
在没有开启缓存时,脚本输出的内容都在服务器端处于等待输出的状态 ,flush()可以将等待输出的内容立即发送到客户端。
开启缓存后,脚本输出的内容存入了输出缓存中 ,这时没有处于等待输出状态的内容,你直接使用flush()不会向客户端发出任何内容。而 ob_flush()的作用就是将本来存在输出缓存中的内容取出来,设置为等待输出状态,但不会直接发送到客户端 ,这时你就需要先使用 ob_flush()再使用flush(),客户端才能立即获得脚本的输出。
1. The correct order of flush and ob_flush is, ob_flush first and then flush, as follows:
ob_flush();
flush();
If the web server If the operating system is Windows, then there will be no problem if the order is reversed or if ob_flush() is not used. [To be verified ] However, the output buffer cannot be refreshed on Linux systems.
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 no longer sent directly to the browser, but is temporarily written to the PHP buffer memory area.
php enables the output_buffering mechanism by default, but by calling the ob_start() function, the data output_buffering value is expanded to a large enough value. You can also specify $chunk_size to specify the value of output_buffering. The default value of $chunk_size is 0, which means that the data in the php buffer will not be sent to the browser until the end of the script. If you set the size of $chunk_size, it means 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 process the data in the buffer by specifying $ouput_callback. For example, the function ob_gzhandler compresses the data in the buffer and then sends it to the browser.
The third parameter: whether to clear the cache, optional, the default is true, if set to false, the cache will not be cleared before the script execution ends.
2.ob_get_contents
Get a copy of the data in the php buffer. It is worth noting that you should call this function before the ob_end_clean() function is called, otherwise ob_get_contents() returns a null character.
You can use ob_get_contents() to obtain the data cached by the server in the form of a string.
Use ob_end_flush() to output the cached data and close the cache.
Using ob_end_clean() will silently clear the data cached on the server without any data or other actions.
The caches on the server side are stacked, which means that after you enable ob_start() and before closing it, you can also enable another cache ob_start() inside it.
But you must also ensure that the number of operations to turn off the cache is the same as the number of operations to turn on the cache.
ob_start() can specify a callback function to process 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 one is The number of ob_start() is B. They each have a callback function called functionA and functionB. When the data in B is output, it will be processed by the funcitonB callback function first, and then handed over to the outer functionA callback function for processing. Then 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 back in the callback function, using The chdir function does not seem to be encountered often. Remember to check the manual when you encounter it.
3. ob_end_flush and ob_end_clean
These two functions are somewhat similar, both turn off the ouptu_buffering mechanism. But the difference is that ob_end_flush only flushes (flush/send) the data in the php buffer to the client browser, while ob_clean_clean clears (erase) the data in the php bufeer but does not send it to the client browser.
Before ob_end_flush is called, the data in the php buffer still exists, and ob_get_contents() can still obtain a copy of the data in the php buffer.
After calling ob_end_flush(), ob_get_contents() gets an empty string, and the browser cannot receive the output, that is, there is no output.
You can use ob_get_contents() to obtain the server-side cached data in string form, and use ob_end_flush() to output the cached data and close the cache.
Using ob_end_clean() will silently clear the data cached on the server without any data or other actions.
The server-side caches are stacked, which means that after you enable ob_start() and before closing it, you can open another cache ob_start() inside it. However, you must also ensure that there are as many operations to turn off the cache as there are operations to turn the cache on.
ob_start() can specify a callback function to process cached data. If one ob_start() is nested inside another ob_start(), we assume that the outer ob_start() has the number A, and the inner ob_start() ) number is B. They each have a callback function, functionA and functionB. Then when the data in cache B is output, it will be processed by the funcitonB callback function first, and then handed over to the outer functionA callback function 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 back in the callback function, using The chdir function does not seem to be encountered often. Remember to check the manual when you encounter it.