ob_clean和ob_flush的区别是什么?

42

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

此外,ob_end_clean()ob_end_flush() 有什么区别?我知道 ob_get_clean()ob_get_flush() 都是获取缓冲区内容并结束输出缓冲。


请查看ob_clean()ob_flush()ob_end_clean()ob_end_flush()的手册页面,或者解释需要详细说明的特定方面。 - mario
3
你所需要了解的一切都在文档中:http://php.net/manual/en/ref.outcontrol.php - dev-null-dweller
我一直在研究它们,看起来没有什么区别。即使在示例中也是如此。 - Alex V
@AlexV 我已经更新了我的回答...但基本上这些函数直接写入输出缓冲区(就像printecho一样),因此您在函数签名中看不到任何区别。 - Adam Wagner
2个回答

62

*_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)

4
换句话说,ob_end_clean() 的意思就是丢弃缓冲区中的所有内容。 - Pacerier

3

关键区别在于*_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()会渲染两次,每个都渲染一次。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接