ob_get_clean 和 ob_get_flush 之间的区别
它们似乎都做同样的事情:将输出缓冲区内容返回给您,然后将其删除。
我应该使用哪一个?
They both seem to do the same thing: return the output buffer content to you and delete it aftewards.
Which one should I use?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
ob_get_clean()
删除缓冲区(不打印它),并返回其内容。ob_get_flush()
打印缓冲区,删除它并返回其内容。这两个函数都会终止缓冲区。
ob_get_clean()
removes the buffer (without printing it), and returns its content.ob_get_flush()
prints the buffer, removes it, and returns its content.Both function will terminate the buffer.
ob_get_clean 只会返回缓冲区的内容并将其分配给您想要的任何变量,但不会输出任何内容。
另一方面,ob_get_flush 执行了 ob_get_clean 执行的所有操作,但它还输出内容。
ob_get_clean
will just return the contents of the buffer and assign it to whatever variable you want it to, but it will not output anything.ob_get_flush
on the other hand, does everything thatob_get_clean
does, but it also outputs the content.这两个函数都会清除输出缓冲区、关闭输出缓冲并返回先前的缓冲区值。
但是,
ob_get_flush
首先将当前缓冲区发送到客户端,而ob_get_clean
只是丢弃它。Both functions clear the output buffer, turn off output buffering, and return the previous buffer value.
However,
ob_get_flush
first sends the current buffer to the client, whereasob_get_clean
just discards it.直接尝试回答您的问题:
如果您希望在刷新缓冲区后再次开始输出缓冲,则使用 ob_get_clean 因为输出缓冲仍将准备就绪,而无需重新打开它。 (请记住,只有在没有文本(甚至空白)回显到浏览器时才能使用此功能)。
因此,对于更一般的用途,我所有的编程书籍都倾向于 ob_get_flush (因为每个脚本只有一个缓冲区)
To directly try to answer your question:
If you wish to begin output buffering again after flushing the buffer, then use ob_get_clean as output buffering will still be ready without having turn it back on. (remember this can only be used if no text, even whitespace is echo'd to the browser).
Thus for more general uses, all my programming books err towards ob_get_flush (as only one buffer per most scripts)