43

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

Which one should I use?

Taz
  • 3,718
  • 2
  • 37
  • 59
EEka
  • 443
  • 1
  • 4
  • 4
  • 8
    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) – Pekka Sep 11 '11 at 16:51
  • 9
    Patience, young padawan! It's been only 6 minutes and 10 page views. – JJJ Sep 11 '11 at 16:55
  • I think the view count doesn't work because I refreshed like 10 times – EEka Sep 11 '11 at 16:56
  • 2
    @EEka: This system is not that naive - remember that it knows who you are :-) – home Sep 11 '11 at 16:58
  • 1
    @Pekka Yup, in the context of php output buffering, *flushing* means actually sending the data. – phihag Sep 11 '11 at 17:03
  • @EEka is now a good time to accept an answer? we are worry about you – a55 Oct 16 '21 at 01:14

4 Answers4

114

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.

Aurovrata
  • 2,000
  • 27
  • 45
Arnaud Le Blanc
  • 98,321
  • 23
  • 206
  • 194
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.

Shef
  • 44,808
  • 15
  • 79
  • 90
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.

phihag
  • 278,196
  • 72
  • 453
  • 469
  • -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. – Adam Oct 26 '12 at 07:17
  • @Adam Great catch, rewrote the answer. Discard and flush are network-level terms, but the new version should clarify that. – phihag Oct 26 '12 at 08:53
-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)

user885983
  • 458
  • 4
  • 9