ob_clean 和 ob_flush 之间的区别?

发布于 2024-12-25 05:00:24 字数 192 浏览 1 评论 0原文

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

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

What's the difference between ob_clean() and ob_flush()?

Also what's the difference between ob_end_clean() and ob_end_flush()? I know ob_get_clean() and ob_get_flush() both get the contents and end output buffering.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫弦 2025-01-01 05:00:24

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

the *_clean variants just empty the buffer, whereas *_flush functions print what is in the buffer (send the contents to the output buffer).

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)
黎夕旧梦 2025-01-01 05:00:24

关键的区别是
*_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() 将渲染两次,每次一次。

The key difference is
*_clean() discards changes and *_flush() outputs to the browser.

Usage of ob_end_clean()

it is mostly used when you want to have a chunk of html and do not want to output to the browser right away but may be used in future.

Eg.

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);

where as ob_end_flush() will render twice, once for each.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文