ob_clean和ob_flush之间的区别?

问题描述 投票:31回答:2

ob_clean()ob_flush()有什么区别?

还有什么区别ob_end_clean()ob_end_flush()?我知道ob_get_clean()ob_get_flush()都获得内容和结束输出缓冲。

php output-buffering
2个回答
50
投票

*_clean变体只是清空缓冲区,而*_flush函数打印缓冲区中的内容(将内容发送到输出缓冲区)。

Example:

ob_start();
print "foo";      // This never prints because ob_end_clean just empties
ob_end_clean();   //    the buffer and never prints or returns anything.

ob_start();
print "bar";      // This IS printed, but just not right here.
ob_end_flush();   // It's printed here, because ob_end_flush "prints" what's in
                  // the buffer, rather than returning it
                  //     (unlike the ob_get_* functions)

0
投票

关键的区别是*_clean()丢弃更改和*_flush()输出到浏览器。

使用ob_end_clean()

它主要用于你想拥有一大块html并且不想立即输出到浏览器但可能在将来使用的时候。

例如。

ob_start()
echo "<some html chunk>";
$htmlIntermediateData = ob_get_contents();
ob_end_clean();

{{some more business logic}}

ob_start();
echo "<some html chunk>";
$someMoreCode = ob_get_content();
ob_end_clean();

renderTogether($htmlIntermediateCode, $someMoreCode);

ob_end_flush()将呈现两次,每次一次。

© www.soinside.com 2019 - 2024. All rights reserved.