Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ob_get_contents + ob_end_clean vs ob_get_clean

Tags:

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.

like image 500
chiliNUT Avatar asked Jul 22 '13 16:07

chiliNUT


People also ask

What is Ob_get_clean?

The ob_get_clean() function returns the contents of the output buffer and then deletes the contents from the buffer.

What does Ob_start () function do?

The ob_start() function creates an output buffer. A callback function can be passed in to do processing on the contents of the buffer before it gets flushed from the buffer. Flags can be used to permit or restrict what the buffer is able to do.


1 Answers

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.

like image 170
Amal Murali Avatar answered Sep 19 '22 01:09

Amal Murali