ob_clean和ob_flush之间的区别?

2022-08-30 12:16:13

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

还有 和 之间的区别是什么?我知道并且都得到了内容和结束输出缓冲。ob_end_clean()ob_end_flush()ob_get_clean()ob_get_flush()


答案 1

变体只是清空缓冲区,而函数打印缓冲区中的内容(将内容发送到输出缓冲区)。*_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)

答案 2

关键区别在于放弃更改并输出到浏览器。*_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() 将呈现两次,每次渲染一次。


推荐