Embedding PHP include statements within a string that gets echoed

526 views Asked by At

I've been trying to clean up my site by defining my site structure as php string variables and then echoing those variables out. This condenses all of my site structure down to five lines with an article in between the echo statements. My page looks like this:

<?php
include('modules/moddefault.php'); //Link to site structure set as variables
$title = "Example Page"; // Define page title. This is inserted into the embedded title statement.

echo "$modtop"; // Declaration, head, title etc.
echo "$precontent"; // Pre-Content Structure
?>

<h1>Web Content Goes Here</h1>
<p>Article content will be nice and uncluttered.</p>

<?php echo "$postcontent"; // Post-Content Structure
?>

I'm wondering how I can embed my CSS includes inside of a PHP string variable. PHP includes don't register because the php has already processed the page. The include statements are being printed to my screen instead of getting processed as a php statement.

Here is a sample of one of the variables I echo in the previous code block:

$precontent = "<body>";
$precontent .= "<div id='wrapper'>";
$precontent .= "<div id='header'>";
$precontent .= "<div>";
$precontent .= "include 'modules/header.inc'"; //THIS IS WHERE I'M HAVING TROUBLE
$precontent .= "</div>";
$precontent .= "</div>";
$precontent .= "<div id='navtop'>";
$precontent .= "<div>";
$precontent .= "include('modules/topnav.inc');"; // THIS IS WHERE I'M HAVING TROUBLE
$precontent .= "</div>";
$precontent .= "</div>";
$precontent .= "<div id='centerbox'>";
$precontent .= "<div id='article'>";
$precontent .= "<div id='innerarticle'>";
$precontent .= "<div>";

I tried embedding another php statement within the string,

$precontent .= "<?php include('modules/topnav.inc';?>"; This didn't work.

I have also tried ssi format, but php seems to ignore those statements because they are written inside an ignore statement.

Any help would be great.

2

There are 2 answers

0
FatherStorm On

have you tried $precontent.=file_get_contents('modules/header.inc');

0
Marc B On

Why not having something like:

header.inc:

   <html>
   <head>
        <title><?php echo $page_title ?></title>
   </head>

   <body>
   [--- common page content here, e.g. menus ---]

and

footer.inc:

   [---common page footer stuff here, eg. copyright lines ---]
   <p><?php echo $page_footer ?></p>
   </body>
   </html>

and then you'd have

article1.php:

<?php
    $page_title = "This is page 1";
    $page_footer = "Thanks for reading this";
    include_once('header.inc');
?>
<p>This is the content of page #1</p>
<?php
    include_once('footer.inc');
?>