ob_clean discards the contents of the output buffer

,



Output buffering allows you to have output of PHP (primarily generated by echo) stored into an memory (ie. buffer) instead of immediately transmitted to the browser or terminal. Which is useful to a variety of tasks:

Preventing output:

ob_start();  // turns on output buffering
$foo->bar();  // all output goes only to buffer
ob_end_clean();  // clears buffer and closes buffering
Capturing the output to a variable:

ob_start();  // turns on output buffering
$foo->render();  // output goes only to buffer
$output = ob_get_contents();  // stores buffer contents to the variable
ob_end_clean();  // clears buffer and closes buffering
Pair ob_get_contents() and ob_end_clean() can be replaced by a single function ob_get_clean(), from whose name disappeared end although it actually turns off output buffering:

$output = ob_get_clean();  // stores buffer content into variable and turns off buffering
In the above examples, the received buffer was not sent to the output. If you want to send it, use ob_end_flush() instead of ob_end_clean(). To obtain the contents of the buffer, send it to the output and turn off buffering, there is again single function (including missing end in the name): ob_get_flush().

Buffer can be emptied at any time without having to turn it off, using ob_clean() (deletes it) or ob_flush() (sends it to output):

ob_start();  // turns on output buffering
$foo->bar();  // all output goes only to buffer
ob_clean();  // delete the contents of the buffer, but remains buffering active
$foo->render(); // output goes to buffer
ob_flush(); // send buffer output
$none = ob_get_contents();  // buffer content is now an empty string
ob_end_clean();  // turn off output buffering

Nesting
Buffers can be nested, so while one buffer is active, another ob_start() activates a new buffer. So ob_end_flush() and ob_flush() are not really sending the buffer to the output, but to the parent buffer. And only when there is no parent buffer, contents is sent to browser or terminal.

It is therefore important to turn off buffering, even if the occurs an exception:

ob_start();
try {
    $foo->render();
} finally {  finally exists since PHP 5.5
    ob_end_clean(); // or ob_end_flush()
}
Buffer size (chunk size)
Buffer may also improve server performace when PHP will not send each echo to the browser, but will send bigger chunks of data, for example, 4KB. Just call at the beginning of the script:

ob_start(null, 4096);
Once the buffer size exceeds 4096 bytes, PHP automatically executes flush, ie. the buffer is emptied and sent out. The same can be achieved by setting directive output_buffering, which is ignored in CLI.

Be careful, if you start buffering without the chunk size (ie. a simple ob_start()) it will cause that the page will not be sent continuously, but once at the end of the script, so the server will respond very sluggishly!



Related Post


Latest Post


Recent Posts Widget

Make sure to never miss a thing...

Get the latest news from the creative industry along with other creative goodies, conveniently delivered to social media.