【问题标题】:Difference between ob_clean and ob_flush?ob_clean 和 ob_flush 的区别?
【发布时间】:2012-01-07 15:52:23
【问题描述】:

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() 的手册页,或者解释需要详细说明的具体方面。
  • 您需要知道的一切都在文档中:php.net/manual/en/ref.outcontrol.php
  • 我一直在研究它们,看起来没有区别。即使在示例中。
  • @AlexV 我已经更新了我的答案......但基本上这些函数直接写入输出缓冲区(就像printecho),因此你会看到没有区别函数签名。

标签: php output-buffering


【解决方案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)

【讨论】:

  • 换句话说ob_end_clean()只是意味着丢弃缓冲区中的所有东西。
【解决方案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() 将渲染两次,每次渲染一次。

【讨论】:

    猜你喜欢
    • 2011-11-13
    • 2015-05-09
    • 1970-01-01
    • 2011-05-10
    • 1970-01-01
    • 1970-01-01
    • 2017-10-14
    相关资源
    最近更新 更多