ob_clean和ob_flush之间的区别?

时间:2012-01-07 15:52:24

标签: php output-buffering

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

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

2 个答案:

答案 0 :(得分:50)

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

实施例

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)

答案 1 :(得分: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()将呈现两次,每个呈现一次。