PHP Doku:: Löscht den Ausgabepuffer - function.ob-clean.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzDas Verhalten von PHP beeinflussenAusgabepufferungOutput-Control-Funktionenob_clean

Ein Service von Reinhard Neidl - Webprogrammierung.

Output-Control-Funktionen

<<flush

ob_end_clean>>

ob_clean

(PHP 4 >= 4.2.0, PHP 5)

ob_clean Löscht den Ausgabepuffer

Beschreibung

void ob_clean ( void )

Diese Funktion verwirft den aktuellen Inhalt des Ausgabepuffers. Anders als bei ob_end_clean() bleibt der Ausgabepuffer dabei aber erhalten.

Siehe auch ob_flush(), ob_end_flush() und ob_end_clean().

Rückgabewerte

Es wird kein Wert zurückgegeben.

Siehe auch

  • ob_flush() - Leert (sendet) den Ausgabepuffer
  • ob_end_flush() - Leert (schickt/sendet) den Ausgabe-Puffer und deaktiviert die Ausgabe-Pufferung
  • ob_end_clean() - Löscht den Ausgabe-Puffer und deaktiviert die Ausgabe-Pufferung


4 BenutzerBeiträge:
- Beiträge aktualisieren...
lev at taintedthoughts dot com
12.06.2007 11:23
I find this function incredibly useful when manipulating or creating images in php (with GD).

I spent quite a while searching through a large number of included files to find where I had a undesired space after php's ending tag - as this was causing all my images on the fly to break 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 know 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 images were being generated, but what I did instead was add this handy function right before my image manipulation code and right after the include/require code.

For example:

<?php

// require some external library files
require ("lib/somelibrary.php");
require (
"lib/class/someclass.php");

// clean the output buffer
ob_clean();

// simple test image
header("Content-type: image/gif");
$im = imagecreate (100, 50);
imagegif($im);
imagedestroy($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 making images in php. As many of you probably already know, extra lines, spacing and padding that appears prior to image-code will prevent the image 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 image from working in the above script.

If you work on an extremely large project with a lot of source and required files, like myself, you will be well-advised to always clear the output buffer prior to creating an image in php.
cipri at php dot net
29.06.2004 18:54
In Re: to Anonymous at 14-Jan-2003.

You can't mimic the behaviour 100% by using that combination of function calls(ob_end_clean() && ob_start()), since outputhandlers may be defined already by the initial ob_start, which may not work as expected when called twice.
kouber at php dot net
11.05.2004 14:05
Although it is mentioned in the manual, you have to be careful when using output buffering in big cycles (such as mass mail sending scripts), because ob_clean() actually does not free any memory, and with each iteration the amount of memory allocated from your script will increase significantly. Instead of calling ob_clean() at the end of the cycle, you have to either use ob_get_clean(), which is a combination of ob_get_contents() and ob_end_clean(), or just ob_end_clean() to free the memory. Try the following test to see the difference:

<?php
for ($i=0; $i<10; $i++) {
 
ob_start();

  echo
"This is iteration $i: ";

 
// * Don't do this!
  // $buf = ob_get_contents();
  // ob_clean();

  // * Use this instead:
 
$buf = ob_get_clean();

  echo
$buf;
 
  echo
memory_get_usage()."\n";
}
?>

14.01.2003 17:23
As far as I can tell the only way to mimic ob_clean()'s behaviour on PHP < 4.2.0 is calling ob_end_clean() followed by ob_start().



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)