PHP 缓冲区 ob_flush() 与flush()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4191385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:15:01  来源:igfitidea点击:

PHP buffer ob_flush() vs. flush()

phpflushoutput-buffering

提问by Ben

What's the difference between ob_flush()and flush()and why must I call both?

什么之间的区别ob_flush(),并flush()和我为什么一定要同时调用?

The ob_flush()referencesays:

ob_flush()参考说:

This function will send the contents of the output buffer (if any).

此函数将发送输出缓冲区的内容(如果有)。

The flush()referencesays:

flush()参考说:

Flushes the write buffers of PHP and whatever backend PHP is using (CGI, a web server, etc).

刷新 PHP 的写入缓冲区以及 PHP 正在使用的任何后端(CGI、Web 服务器等)。

However, it continues to say:

然而,它继续说:

[it] may not be able to override the buffering scheme of your web server…

[它] 可能无法覆盖您的 Web 服务器的缓冲方案......

So, seems to me that I could just use ob_flush()all of the time. However, I get strange results when I do that. Could someone explain in simple terms what's going on here?

所以,在我看来,我可以一直使用ob_flush()。但是,当我这样做时,我得到了奇怪的结果。有人可以用简单的术语解释这里发生了什么吗?

采纳答案by mario

ob_flushsends an application-initiated buffer. There may be multiple nested ob_start()'s in any PHP script. ob_flushpasses the current content to the upper layer.

ob_flush发送应用程序启动的缓冲区。ob_start()任何 PHP 脚本中都可能有多个嵌套的 。ob_flush将当前内容传递给上层。

PHP itself might (at its own discretion) buffer output. This depends on the back-end. But usually FastCGIhas a socket buffer on its own. Therefore flush()needs to be invoked as well to send the current content to the web server.

PHP 本身可能(自行决定)缓冲输出。这取决于后端。但通常FastCGI有自己的套接字缓冲区。因此也flush()需要调用以将当前内容发送到 Web 服务器。

And now the web server might itself implement another buffering scheme (mod_deflateor content filter), which you have no influence over. But this is seldom, as it needs to be configured specifically.

现在 Web 服务器本身可能会实现另一种缓冲方案(mod_deflate或内容过滤器),您对此没有任何影响。但这很少见,因为它需要专门配置。

Anyway, use both.

总之,两者都用。

回答by Dan Grossman

ob_flushflushes output buffers youcreated with a function like ob_start

ob_flush使用类似的函数刷新创建的输出缓冲区ob_start

flushflushes buffered output of the PHP script itself to its caller

flush将 PHP 脚本本身的缓冲输出刷新到其调用者

回答by Lightness Races in Orbit

ob_flush()is a high-level flush. It flushes high-level buffers and puts all content in the low-level, internal buffers ready to send.

ob_flush()是高级同花顺。它刷新高级缓冲区并将所有内容放在准备发送的低级内部缓冲区中。

  • Note that the ob_family of functions create stacksof buffers, so just blindly writing ob_flush()everywhere is indeed going to give you "strange results" if the code was written to take advantage of this stacking.
  • 请注意,ob_函数系列创建了缓冲区堆栈,因此ob_flush()如果编写代码以利用这种堆栈,那么盲目地到处编写确实会给您带来“奇怪的结果”。

flush()is a low-level flush, instructing PHP to flush its internal, low-level data buffers.

flush()是低级刷新,指示 PHP 刷新其内部的低级数据缓冲区。

Below that still, there will be socket-layer buffers; below that, there are network-layer buffers. And, at the lowest level, the queue of electrons going down the data cable.

在此之下,将有套接字层缓冲区;在其下方,有网络层缓冲区。并且,在最低层,沿着数据线的电子队列。

回答by bcosca

I guess this is in relation to your previous question. The significant advantage of using output buffering is when it's used alongside data compression. If you're not using ob_gzhandler, there's little to gain. flushalone will just commit whatever output data is still on the server. With ob_startand its counterparts ob_flush, ob_end_cleanand ob_end_flush, whatever is waiting to be compressed (look at flushand ob_flushas referring to different buckets - obsends data to flush, flushsends data to browser - may not be accurate but that's the idea) will be wrapped up and sent to the client.

我想这与您之前的问题有关。使用输出缓冲的显着优势是当它与数据压缩一起使用时。如果您不使用ob_gzhandler,则没有什么好处。flush单独提交任何仍在服务器上的输出数据。Withob_start和它的对应物ob_flush, ob_end_cleanand ob_end_flush,任何等待被压缩的东西(看看flushob_flush指的是不同的存储桶 -ob将数据发送到flushflush将数据发送到浏览器 - 可能不准确,但这就是想法)将被包装并发送到客户端.