Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
398 views
in Technique[技术] by (71.8m points)

php - ob_get_contents + ob_end_clean vs ob_get_clean

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To answer your question:

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

Yes. It is functionally equivalent.


Case 1:

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

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

Case 2:

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...