Problem with echo'ing a php variable in a simple template

Hi all,

I was reading through the sitepoint book on php and mysql, and I was trying to write a simple template as per chapter 4 but I’m having a problem. A snippet of code is shown below, its probably something silly I’m doing wrong

index.php


    // $page is decoded from url and is verified to be as expected
    //... some other stuff
    $title = '$page';
    include 'page.tpl.php';

page.tpl.php


<html>
<title>
<?php echo $title; ?>
</title>
</html>

However the title is always blank - what am I doing wrong?

$page hasnt been defined as anything in your code, thats why its alway blank, I suspect the code in the book was more an example than a working model. :wink:

Add this line to the top of your index.php


$page = 'this is the page title';

you also want to remove the quotes around $page, that makes it a string not a variable

Variables aren’t expanded in single quotes, not that quotes are needed here.

Try


$title = $page;

thanks I think thats probably it, if its not I’ll let you know.

Many thanks again!