Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between ob_clean and ob_flush?

What's the difference between ob_clean() and ob_flush()?

Also what's the difference between ob_end_clean() and ob_end_flush()? I know ob_get_clean() and ob_get_flush() both get the contents and end output buffering.

like image 202
Alex V Avatar asked Jan 07 '12 15:01

Alex V


People also ask

When to use ob_ clean?

Definition and Usage The ob_clean() function deletes all of the contents of the topmost output buffer, preventing them from getting sent to the browser.

What is PHP Ob_end_flush?

The ob_end_flush() function deletes the topmost output buffer and outputs all of its contents. The output may be caught by another output buffer, or, if there are no other output buffers, sent directly to the browser.

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 is output buffering in PHP?

Output buffering is a mechanism for controlling how much output data (excluding headers and cookies) PHP should keep internally before pushing that data to the client. If your application's output exceeds this setting, PHP will send that data in chunks of roughly the size you specify.


2 Answers

the *_clean variants just empty the buffer, whereas *_flush functions print what is in the buffer (send the contents to the output buffer).

Example:

ob_start(); print "foo";      // This never prints because ob_end_clean just empties ob_end_clean();   //    the buffer and never prints or returns anything.  ob_start(); print "bar";      // This IS printed, but just not right here. ob_end_flush();   // It's printed here, because ob_end_flush "prints" what's in                   // the buffer, rather than returning it                   //     (unlike the ob_get_* functions) 
like image 152
Adam Wagner Avatar answered Sep 24 '22 11:09

Adam Wagner


The key difference is *_clean() discards changes and *_flush() outputs to the browser.

Usage of ob_end_clean()

it is mostly used when you want to have a chunk of html and do not want to output to the browser right away but may be used in future.

Eg.

ob_start() echo "<some html chunk>"; $htmlIntermediateData = ob_get_contents(); ob_end_clean();  {{some more business logic}}  ob_start(); echo "<some html chunk>"; $someMoreCode = ob_get_content(); ob_end_clean();  renderTogether($htmlIntermediateCode, $someMoreCode); 

where as ob_end_flush() will render twice, once for each.

like image 39
Arun Gangula Avatar answered Sep 23 '22 11:09

Arun Gangula