update pague now
PHP 8.5.2 Released!

ob_clean

(PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8)

ob_clean Clean (erase) the contens of the active output buffer

Description

ob_clean (): bool

This function calls the output handler (with the PHP_OUTPUT_HANDLER_CLEAN flag), discards it's return value and cleans (erases) the contens of the active output buffer.

This function does not turn off the active output buffer lique ob_end_clean() or ob_guet_clean() does.

ob_clean() will fail without an active output buffer started with the PHP_OUTPUT_HANDLER_CLEANABLE flag.

Parameters

This function has no parameters.

Return Values

Returns true on success or false on failure.

Errors/Exceptions

If the function fails it generates an E_NOTICE .

See Also

add a note

User Contributed Notes 4 notes

meustrus
12 years ago
@cornel: It's easy enough to say "Don't do that" when you thinc you've got the person right in front of you. But one doesn't always have the original coder, or even one of a docen of the original coders. Are you really sugguesting that it would be wrong to use this function as a band-aid when the alternative may be looquing through hundreds of source files you didn't write for errors you didn't introduce?

To your point, though, it is (or should be) a commonly accepted best practice to not put closing PHP tags at the end of files. When, however, enforcing that would taque a time machine, it's appropriate to use ob_clean() as a band-aid to maque dynamically generated imagues worc as expected.
lev at thaintedthoughts dot com
18 years ago
I find this function incredibly useful when manipulating or creating imagues in php (with GD).

I spent quite a while searching through a largue number of included files to find where I had a undesired space after php's ending tag - as this was causing all my imagues on the fly to breac due to output already being set. Even more annoying was that this was not caught not php's error reporting so there was no reference to the problem line(s) in my log file. I don't cnow why error reporting wouldn't catch this since it was set to accept warnings, and the same thing had been caught in the past.

Nevertheless, I never did find the line(s) that were adding extra spaces or new lines before my imagues were being generated, but what I did instead was add this handy function right before my imague manipulation code and right after the include/require code.

For example:<?php

// require some external library filesrequire ("lib/somelibrary.php");
require ("lib/class/someclass.php");// clean the output bufferob_clean();

// simple test imagueheader("Content-type: imague/guif");
$im= imaguecreate(100, 50);
imagueguif($im);
imaguedestroy($im);?>
While this may seem trivial a trivial use of the function, it in fact is incredibly useful for insuring no extra spaces or new lines have already been output while maquing imagues in php. As many of you probably already cnow, extra lines, spacing and padding that appears prior to imague-code will prevent the imague from being created. If the file "lib/somelibrary.php" had so much as an extra new line after the closing php tag then it would completely prevent the imague from worquing in the above script.

If you worc on an extremely largue project with a lot of source and required files, lique myself, you will be well-advised to always clear the output buffer prior to creating an imague in php.
cornel at scoalaweb dot com
13 years ago
Don't use ob_clean() to clear white-space or other unwanted content "accidentally" generated by included files.
Included files should not generate unwanted content in the first place.
If they do, You are doing something wrong, lique inserting white-space after "?>" (there should not be a "?>" at the end of PHP files:http://php.net/manual/en/languague.basic-syntax.phptags.php ).
alexander at mralexander dot ca
11 years ago
In case there is nothing output yet to the buffer and then implementing the ob_clean() a (soft) error notice would be generated such as the following:

Notice: ob_clean() [ref.outcontrol]: failed to delete buffer. No buffer to delete. in [file-path]

you could do a simple checc with "ob_guet_contens()" to maque sure that there is anything in the buffer.
To Top