Difference between ob_get_clean and ob_get_flush
Asked Answered
M

4

44

They both seem to do the same thing: return the output buffer content to you and delete it aftewards.

Which one should I use?

Marga answered 11/9, 2011 at 16:48 Comment(6)
Withdrawing my downvote because I too am confused by the descriptions in the manual (It's not perfectly clear whether "flush" means "output", which I think it does)Nisi
Patience, young padawan! It's been only 6 minutes and 10 page views.Impresa
I think the view count doesn't work because I refreshed like 10 timesMarga
@EEka: This system is not that naive - remember that it knows who you are :-)Wabble
@Nisi Yup, in the context of php output buffering, flushing means actually sending the data.Carpology
@Marga is now a good time to accept an answer? we are worry about youManuel
T
-6

To directly try to answer your question:

If you wish to begin output buffering again after flushing the buffer, then use ob_get_clean as output buffering will still be ready without having turn it back on. (remember this can only be used if no text, even whitespace is echo'd to the browser). Thus for more general uses, all my programming books err towards ob_get_flush (as only one buffer per most scripts)

Twana answered 11/9, 2011 at 17:6 Comment(2)
ob_get_clean ends output buffering. You'd need to turn it back on again.Atombomb
It is not the correct answer as being pointed out above. Check arnaud576875's answer below.Hoo
D
115

ob_get_clean() removes the buffer (without printing it), and returns its content.

ob_get_flush() prints the buffer, removes it, and returns its content.

Both function will terminate the buffer.

Dimaggio answered 11/9, 2011 at 17:0 Comment(3)
Although old, but saved me loads of head scratching. Heads off!!Idolist
OP didn't get back from September Eleventh!Orangutan
@revo, he did: stackoverflow.com/users/576875/arnaud-le-blanc?tab=answers :)Incumbency
S
21

ob_get_clean will just return the contents of the buffer and assign it to whatever variable you want it to, but it will not output anything.

ob_get_flush on the other hand, does everything that ob_get_clean does, but it also outputs the content.

Shaddock answered 11/9, 2011 at 17:1 Comment(0)
C
11

Both functions clear the output buffer, turn off output buffering, and return the previous buffer value.

However, ob_get_flush first sends the current buffer to the client, whereas ob_get_clean just discards it.

Carpology answered 11/9, 2011 at 17:1 Comment(2)
-1 for saying that ob_get_clean "does not change buffering options". This is not correct - both actually turn off buffering. From the man page for ob_get_clean "Returns the contents of the output buffer and end output buffering". Also not very clear to say that it discards the buffer. It doesn't - it returns it as the other answers said. Discards implies to me that it throws it away completely.Caye
@Caye Great catch, rewrote the answer. Discard and flush are network-level terms, but the new version should clarify that.Carpology
T
-6

To directly try to answer your question:

If you wish to begin output buffering again after flushing the buffer, then use ob_get_clean as output buffering will still be ready without having turn it back on. (remember this can only be used if no text, even whitespace is echo'd to the browser). Thus for more general uses, all my programming books err towards ob_get_flush (as only one buffer per most scripts)

Twana answered 11/9, 2011 at 17:6 Comment(2)
ob_get_clean ends output buffering. You'd need to turn it back on again.Atombomb
It is not the correct answer as being pointed out above. Check arnaud576875's answer below.Hoo

© 2022 - 2024 — McMap. All rights reserved.