ob_end_clean

Description

boolob_end_clean
( void
)

This function discards the contents of the topmost output buffer and turns
off this output buffering. If you want to further process the buffer's
contents you have to call ob_get_contents() before
ob_end_clean() as the buffer contents are discarded
when ob_end_clean() is called.

Return Values

Returns TRUE on success or FALSE on failure. Reasons for failure are first that you called the
function without an active buffer or that for some reason a buffer could
not be deleted (possible for special buffer).

Errors/Exceptions

If the function fails it generates an E_NOTICE.

Examples

The following example shows an easy way to get rid of all output buffers:

Example #1 ob_end_clean() example

<?phpob_start();echo 'Text that won\'t get displayed.';ob_end_clean();?>

In addition to John Smith's comment (#42939), ob_gzhandler() may still set the HTTP header "Content-Encoding" to "gzip" or "deflate" even if you call ob_end_clean(). This will cause a problem in the following situation:

In reference to <geoff at spacevs dot com> where he states, "If you call ob_end_clean in a function registered with 'register_shutdown_function', it is too late, any buffers will have already been sent out to the client.", here is a workaround I came up with.

<?php

function ClearBuffer($Buffer) { return "";}

function Shutdown() {ob_start("ClearBuffer");}

register_shutdown_function("Shutdown");

?>

This will wipe out all the contents of the output buffer as it comes in. Basically its the same as "STDOUT > /dev/null".

Instead of checking the If-Modified-Since-Header against the date of the last modification of the script, you can of course query a database or take any other date that is somehow related to the modification of the result of your script.

You can for instance use this technique to generate images dynamically. If the user indicates he already has a version of the image by the If-Modified-Since-Header, there's no need to generate it and let the server finally discard it because the server only then interpretes the If-Modified-Since-Header.This saves server load and shortens response-times.