note 55517 added to function.ob-start

From: Date: Fri, 05 Aug 2005 14:54:27 +0000
Subject: note 55517 added to function.ob-start
Groups: php.notes 
Request: Send a blank email to php-notes+get-93136@lists.php.net to get a copy of this message
I usually create my pages in four parts - variable initialisation, import header (using the variables just declared to configure), main body (mostly non-PHP), import footer. I wondered about making the main body examinable by another PHP script if the main page was included into it. I found I could control output of the main body by ending the header with an unclosed function which finishes at the start of the footer, thus enclosing the main body. Output buffering can then be used to read this into a variable. As a demonstration of how this can be used to control the order of output look at this example: <?php $output = ""; // Callback to process buffered output function capture($buffer) { $GLOBALS['output'] .= $buffer; return "C "; } // Calls the printE() function with output capture function captureE() { ob_start("capture"); printE(); ob_end_flush(); } ?> A <?php // Output 'E' (the main body in the example scenario) function printE() { // (End header after this line) ?> E <?php // (Start footer with this line) } ?> B <?php captureE(); ?> D <?php print $output; ?> F <?php printE(); ?> G The output is A B C D E F E G. For the application I mentioned above there are two points to note: - The page when executed alone must output its main body but the inspection script should suppress this, perhaps by means of a variable set before the page is included and then checked for in the footer output lines. - Because the main body is now inside a function it has a different namespace, thus changes may be required to prevent code breaking (e.g. use of globals, handling of functions defined within the main body). ---- Manual Page -- http://www.php.net/manual/en/function.ob-start.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+55517 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+55517&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+55517&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+55517&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+55517&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+55517&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+55517&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+55517&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#93136) next »