Sunday, June 02, 2002, 7:55:09 PM, Andre wrote:
AD> I've been wondering why code in 'pure' php? Is there some compelling reason
AD> (that I'm unaware of) for doing so? Should I rewrite all my earlier code into 

        Sorry to chime in so late, but IMO, be more concerned with
saving output for the last step in execution, because once it's echoed
out, there's no turning back or sending headers or a redirect..  Have
functions return strings instead of echo them (and take advantage of
"<<<" syntax for more readable string building), build up a document
into variables, send out at very end.

Some home-grown example code might be useful:


//setup session, DB, some common functions

// start building string array for later output
$Template["title"] = "query results";

// if DB problems, load content, output, quit.
if (!$dbi->connect()) {
    $Template["main_content"] =

// build result table HTML
$result_table = open_result_table();
$dbi->run_query("SELECT ..");
while ($row = $dbi->next_row_array()) {
    $result_table .= <<<EOD
$result_table .= "</table>\n";

// load results content
$results_copy = join('',file("about_results.html",1));

$Template["main_content"] .= <<<EOD
    <h1>Query Results</h1>
    <h2>About Results</h2>

if (//need to go elsewhere) {
    header("Location: http://elsewhere";);

$_SESSION['last_accessed'] = $PHP_SELF;




Point is, at any point in execution cookies or headers can be sent or
content changed.  You end up with most content in seperate files and
script logic/execution very easy to understand/debug.  Stick a "watch"
echo anywhere in the code and it pops up at the top of the browser

For the above code, main_template.php is a full HTML document (only
HTML editor needed) with PHP tags in just a few spots to echo elements
of the $Template array:

<!DOCTYPE ...>
    <title><?php echo $Template['title'] ?></title>
    <?php echo $Template['main_content'] ?>

Bogdan wrote:
> There's an urban legend saying that switching php tags on and off would
> slow parsing down.

Content outside PHP tags gets to skip the parser.  From the manual:

"..for outputting large blocks of text, dropping out of PHP parsing mode
is generally more efficient than sending all of the text through
echo() or print() or somesuch."


PHP General Mailing List (
To unsubscribe, visit:

Reply via email to