9

i am confused about the PHP functions ob_flush() and ob_end_flush(). About the function ob_flush the manual says

The buffer contents are discarded after ob_flush() is called.This function does not destroy the output buffer like ob_end_flush() does. 

i am confused about the words discarded and destroyed here. Even if the buffer contents are discarded in case of ob_flush() they cant be accessed and even if they are destroyed as in case of ob_end_flush() they cant be accessed. Then whats the difference between these two functions?

UPDATE:

In response to JamWaffles answer I dont understand the significance of deleting everything in the buffer but keeping the buffer vs deleting the whole buffer(freeing it) because PHP has no concept of pointers and you cant get the address of buffers so it should be immaterial whether you keep the empty buffer with you or you free it

Community
  • 1
  • 1
lovesh
  • 5,235
  • 9
  • 62
  • 93

3 Answers3

22

I think in this case they mean the same thing. ob_flush() is used when you want to flush parts of the page to the client, whereas ob_end_flush() flushes the entire buffer, then destroys the buffer. What ob_flush() does is delete everything in the buffer, but keeps the buffer itself so more data can be put into it after the ob_flush() call.


I'll try to explain better.

Discarded

Let's say I have a nice, bright orange plastic bucket. This is my buffer. I then get some sand, representing the contents of the buffer, and fill the buffer (bucket) up. I then pick this bucket with sand in it up and pour it into a sandpit, which is my client. You'll notice the sand is gone, yet the bucket remains. This is what is meant by the buffer contents are discarded - the buffer itself can be re-used (filled up with sand again). In memory terms, the memory is emptied but not freed, so it can be filled again.

Destroyed

Now, if we take our bucket again, fill it up with sand once more, empty the sand out and then set fire to the bucket because we don't require it any longer, that's called destroying the buffer; the data in the buffer is gone, but so is the buffer itself. In memory terms, the memory is freed for other use.


Is this significant in PHP, without pointers, the OP asks? Well, it depends what you want to do. If you're processing a long page, and want to (for example) send the header and sidebar to the client while you process the rest of the page for sending after it's done, use ob_flush().

If you want to flush something to the client without any more output after it, use ob_end_flush().


I mean absolutely no disrespect in talking in a rather patronising tone; I wanted to make an analogy to make the definitions as clear as possible.

Community
  • 1
  • 1
Bojangles
  • 99,427
  • 50
  • 170
  • 208
  • "delete everything in the buffer, but keeps it"? how can you keep it when u delete it? sorry i didnt get you – lovesh Sep 08 '11 at 22:49
  • when you say `ob_flush()` flushes parts of the page,do you mean that it flushes only innermost buffer in case of nested output buffers, while `ob_end_flush()` flushes all buffers instantly even if there is nesting of `ob_start()`? – lovesh Sep 08 '11 at 22:55
  • Sorry about that! I've edited my question. In response to your second comment, I mean that `ob_flush()` allows more output to be buffered and sent to the client, whereas `ob_end_flush()` doesn't. – Bojangles Sep 08 '11 at 23:03
  • sorry but still i dont get u. whats more output and wats less output. Is there any size limit? sorry but that is difficult for me to groke.and about your edit in your answer i ll edit my question to better communicate what i dont understand about that. – lovesh Sep 08 '11 at 23:08
  • please see my update to the question. and sorry i m not good at expressing myself so it might be difficult to understand – lovesh Sep 08 '11 at 23:15
  • i understood what you meant by discarded and destroyed. But is it significant in a language like PHP with no pointers?Thanks – lovesh Sep 08 '11 at 23:22
  • 1
    It depends what you want to do. If you're not echoing anything else after the flush call, use `ob_end_flush()`; this ends the script output, whereas `ob_flush()` allows more output to be sent to the client. – Bojangles Sep 08 '11 at 23:32
5

ob_flush does not turn off output buffering

yhager
  • 1,632
  • 15
  • 16
1

ob_end_flush() displays everything from the buffer, then destroys the buffer. ob_flush does the same, but does not destroy the buffer just clears it.

ob_flush() =

ob_end_flush();
ob_start();
Dan Bray
  • 7,242
  • 3
  • 52
  • 70