Dear PHP-ologists,

For a variety of reasons (including legacy code) I have a number of websites that (in a nutshell) more or less do the following:

In index.php:
include ('templates/header.php');
include ('content/homepage.php');
include ('templates/footer.php');

In header.php:
echo $headerstuff_line1 . "\n";
echo $headerstuff_line2 . "\n";
echo $headerstuff_line3 . "\n";
# etc...

(and footer.php works similarly of course).

In home.php:
echo '<h1>Hello world</h1>' . "\n";
echo 'Content line 1...' . "\n";
echo 'Content line 2...' . "\n";
echo 'Content line 3...' . "\n";
# etc...

And home.php (or any other page that gets included here, depending on the parsing of query parameters) may contain other stuff as well, such as SQL queries, invocation of E-mail / captcha / form / other classes, and what not. Crude but effective.

But now I am faced with a need to modify the page header include file on the basis of variables that get defined in the content include file. For example in home.php there could be something like

$pageTitle = 'Home';

which would then have to be used in the header.php include, as in:

echo '<title>' . $pageTitle . </title>' . "\n";

Which means that I have no shot myself in the foot, since the header has been included (and has executed it's 'echo' statements) before the content file (with the declarations needed in the header) is loaded up only after that.

Fixing it involves breaking my straightforward process flow - i.e. I would have to put the statements that output the header into the content include (e.g. home.php) which I find highly inelegant... Or even worse, I would have to store entire chunks of HTML content in large memory-consuming variables, manipulate them later, and output them only after modification, which also breaks my straightforward process flow _and_ means higher server load and delayed output and other impediments to performance that I don't want.

So. Suggestions anyone? Clearly I need to so something very simple and sensible here, but i just can't see it. Any idea's would be appreciated!

// Frank