ob_get_contents + ob_end_clean与ob_get_clean [英] ob_get_contents + ob_end_clean vs ob_get_clean

查看:76
本文介绍了ob_get_contents + ob_end_clean与ob_get_clean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这两段PHP之间有什么区别吗?

Is there any difference between these two pieces of PHP?

ob_start();
//code...
$pageContent = ob_get_contents();
ob_end_clean();
someFunction($pageContent);

vs

ob_start();
//code...
$pageContent=ob_get_clean();
someFunction($pageContent);

我当前正在使用第一个块,但是如果功能上等效,我想使用第二个块,因为它更加简洁.

I am currently using the first block, but I would like to use the second instead if it is functionally equivalent, since it is a bit more concise.

推荐答案

要回答您的问题:

ob_get_clean()本质上执行ob_get_contents()和ob_end_clean().

ob_get_clean() essentially executes both ob_get_contents() and ob_end_clean().

是的.在功能上是等效的.

Yes. It is functionally equivalent.

案例1:

ob_get_contents() + ob_end_clean():

ob_get_contents —返回输出缓冲区的内容

ob_get_contents — Return the contents of the output buffer

ob_end_clean —清理(擦除)输出缓冲区并关闭输出缓冲

ob_end_clean — Clean (erase) the output buffer and turn off output buffering

因此,基本上,您是将输出缓冲区的内容存储到一个变量中,然后使用ob_end_clean()清除它.

So, basically, you're storing the contents of the output buffer to a variable and then clearing it with ob_end_clean().

案例2:

ob_get_clean —获取当前缓冲区内容并删除当前输出缓冲区

ob_get_clean — Get current buffer contents and delete current output buffer

您要将缓冲区内容存储到变量中,然后删除输出缓冲区.

You're storing the buffer contents to a variable and then the output buffer is deleted.

您所做的基本上是相同的.因此,在这里使用第二个代码块不会出现任何问题,因为它们都在做相同的事情.

What you're doing is essentially the same. So, I don't see anything wrong with using the second code-block here, since they're both doing the same thing.

这篇关于ob_get_contents + ob_end_clean与ob_get_clean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆