PHP Buffer Ob_Flush() Vs. Flush()

PHP buffer ob_flush() vs. flush()

ob_flush sends an application-initiated buffer. There may be multiple nested ob_start()'s in any PHP script. ob_flush passes the current content to the upper layer.

PHP itself might (at its own discretion) buffer output. This depends on the back-end. But usually FastCGI has a socket buffer on its own. Therefore flush() needs to be invoked as well to send the current content to the web server.

And now the web server might itself implement another buffering scheme (mod_deflate or content filter), which you have no influence over. But this is seldom, as it needs to be configured specifically.

Anyway, use both.

whats the difference between ob_flush and ob_end_flush?

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.

Correct use of ob_flush function in php

you need to start your output-buffer first:

<?php
ob_start(); //start output buffering here
//everything from here on is added to the buffer

echo '<html><body> .. blah blah ... ';
?>

more text or html code here

<?php
ob_flush(); //output everything buffered, end output buffering
?>

Difference between ob_clean and ob_flush?

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)

failed to flush buffer. No buffer to flush with ob_end_flush() method

So, looking at the PHP source for ob_flush(), which is where the "failed to flush buffer. No buffer to flush" error is being generated, it appears that error is generated if there's no active output buffer.

Based on the PHP source, ob_end_flush() should never result in that error - it can't, it's not in the code. Are you sure you don't have an ob_flush() somewhere in your code that you're not including in what you're sharing with us in your question?

Anyway, you're not checking the return value of ob_start() - it can fail, and if it fails to create a new output buffer, then a subsequent ob_flush() or ob_end_flush() will fail because those commands require an active output buffer in order to function, and if there isn't one, they will emit errors.

You can see in the PHP source for ob_end_flush() that the error it emits when there is no active output buffer is "failed to delete and flush buffer. No buffer to delete or flush".

Is the output buffer flushed automatically?

The output is flushed automatically, you can test it with a simple script like this:

<?php
ob_start();
echo "Hello";
?>

The performances are the same, since it's flushed automatically.



Related Topics



Leave a reply



Submit