note 42290 added to function.ob-clean

From: Date: Tue, 11 May 2004 09:05:27 +0000
Subject: note 42290 added to function.ob-clean
Groups: php.notes 
Request: Send a blank email to php-notes+get-69442@lists.php.net to get a copy of this message
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"; } ?> ---- Manual Page -- http://www.php.net/manual/en/function.ob-clean.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+42290 Delete -- http://master.php.net/manage/user-notes.php?action=delete+42290&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+42290&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#69442) next »