php ob_get_contents + ob_end_clean 与 ob_get_clean
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17792817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ob_get_contents + ob_end_clean vs ob_get_clean
提问by chiliNUT
Is there any difference between these two pieces of PHP?
这两个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.
我目前正在使用第一个块,但如果它在功能上等效,我想改用第二个块,因为它更简洁一些。
回答by Amal Murali
To answer your question:
回答你的问题:
ob_get_clean() essentially executes both ob_get_contents() and ob_end_clean().
ob_get_clean() 本质上同时执行 ob_get_contents() 和 ob_end_clean()。
Yes. It is functionally equivalent.
是的。它在功能上是等效的。
Case 1:
情况1:
ob_get_contents()
+ ob_end_clean()
:
ob_get_contents()
+ ob_end_clean()
:
ob_get_contents — Return the contents of the output buffer
ob_end_clean — Clean (erase) the output buffer and turn off output buffering
ob_get_contents — 返回输出缓冲区的内容
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()
.
因此,基本上,您将输出缓冲区的内容存储到一个变量中,然后使用ob_end_clean()
.
Case 2:
案例2:
ob_get_clean — Get current buffer contents and delete current output buffer
ob_get_clean — 获取当前缓冲区内容并删除当前输出缓冲区
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.
你所做的基本上是一样的。所以,我认为在这里使用第二个代码块没有任何问题,因为它们都在做同样的事情。
回答by jor
ob_get_contents()
can be used to continue the output buffering.
ob_get_contents()
可用于继续输出缓冲。
Example:
例子:
ob_start();
echo 'Something!';
$html1 = ob_get_contents();
echo 'More to say!';
$html2 = ob_get_contents();
ob_end_clean();
At the end the vars have this content:
最后,vars 具有以下内容:
$html1 = 'Something!';
$html2 = 'Something!More to say!';
回答by Mark Amery
There is one teeny difference between
之间有一个微小的区别
$stuff = ob_get_clean();
and
和
$stuff = ob_get_contents();
ob_end_clean();
which is that the latter will throw an E_NOTICE
if there is no active output buffer at the time that you call it, and the former won't. Throwing the notice actually seems like the saner behaviour to me, since if you're calling these functions without an output buffer then you're probably doing something wrong!
也就是说,E_NOTICE
如果在您调用它时没有活动的输出缓冲区,后者将抛出一个,而前者则不会。抛出通知实际上对我来说似乎是更明智的行为,因为如果您在没有输出缓冲区的情况下调用这些函数,那么您可能做错了什么!
That the two approaches are pretty much equivalent is explicitly documented on php.net, which says:
php.net 上明确记录了这两种方法几乎等效,其中说:
ob_get_clean()
essentially executes bothob_get_contents()
andob_end_clean()
.
ob_get_clean()
本质上同时执行ob_get_contents()
和ob_end_clean()
。
The warning-throwing behaviour of ob_end_clean
is also documented:
的警告投掷行为ob_end_clean
是还记载:
If the function fails it generates an
E_NOTICE
.
如果函数失败,它会生成一个
E_NOTICE
.
Note that there is no similar sentence in the docs of ob_get_contents
or ob_end_clean
.
请注意,ob_get_contents
或的文档中没有类似的句子ob_end_clean
。
If you really want to assure yourself there are no further differences between these functions (there aren't), you can dive into the definitions of ob_get_contents
, ob_end_clean
and ob_get_clean
in the source. There's some weird error handling for impossible cases in ob_get_clean
that should never get reached, but besides that, you can see that the behaviours are as described.
如果您真的想确保自己在这些函数之间没有进一步的区别(没有),您可以深入了解ob_get_contents
,ob_end_clean
和ob_get_clean
源代码中的定义。对于不可能发生的情况,有一些奇怪的错误处理,ob_get_clean
永远不会达到,但除此之外,您可以看到行为如所描述的那样。
回答by Ryszard J?draszyk
ob_get_contents() + ob_end_clean()
ob_get_contents() + ob_end_clean()
Is supposed to work the same as:
应该与以下内容相同:
ob_get_clean
ob_get_clean
However, because of PHP bug, it doesn't. Roland from nextendweb filed a bug report:
但是,由于 PHP 错误,它没有。来自 nextendweb 的 Roland 提交了一个错误报告:
https://bugs.php.net/bug.php?id=76563
https://bugs.php.net/bug.php?id=76563
and suggests to use:
并建议使用:
$content = ob_get_contents();
ob_clean();
instead of:
代替:
$content = ob_get_clean();
Testing on sandbox, you will notice that it affects all PHP versions.
在沙箱上进行测试,您会注意到它会影响所有 PHP 版本。
ob_get_clean
broke a few plugins using output buffer on my staging website.
ob_get_clean
在我的登台网站上使用输出缓冲区破坏了一些插件。
回答by Ravi Patel
ob_get_clean()function executes both ob_get_contents()and ob_end_clean().
ob_get_clean()函数同时执行ob_get_contents()和ob_end_clean()。
Same functionality as ob_get_contents()+ ob_end_clean(), ob_get_clean()returns the contents of the output buffer and end output buffering. If output buffering isn't active then FALSEis returned.
与ob_get_contents()+ ob_end_clean() 的功能相同,ob_get_clean()返回输出缓冲区的内容并结束输出缓冲。如果输出缓冲未激活,则返回FALSE。