问题描述
这两段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.
推荐答案
要回答您的问题:
是的.在功能上是等效的.
Yes. It is functionally equivalent.
案例1:
ob_get_contents()
+ ob_end_clean()
:
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:
您要将缓冲区内容存储到变量中,然后删除输出缓冲区.
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!