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 

Andre,
        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:

<?php

//setup session, DB, some common functions
require_once("setup_session.php");
require_once("create_DB_object_instance.php");
require_once("html_building_functions.php");

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

// if DB problems, load content, output, quit.
if (!$dbi->connect()) {
    $Template["main_content"] =
        join('',file("DB_down_message.html",1));
    include("main_template.php");
    exit(0);
}

// build result table HTML
$result_table = open_result_table();
$dbi->run_query("SELECT ..");
while ($row = $dbi->next_row_array()) {
    $result_table .= <<<EOD
    <tr>
        <td>{$row["guns"]}</td>
        <td>{$row["butter"]}</td>
    </tr>
EOD;
}
$result_table .= "</table>\n";

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

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

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

$_SESSION['last_accessed'] = $PHP_SELF;

setcookie(//something);

include("main_template.php");

?>

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
window.

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 ...>
<html>
  <head>
    <title><?php echo $Template['title'] ?></title>
  </head>
  <body>
    <?php echo $Template['main_content'] ?>
  </body>
</html>

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."

Steve
-- 
[EMAIL PROTECTED] ** http://mrclay.org


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to