Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP Build a Basic PHP Website Building a Media Library in PHP Adding Active States to the Navigation

How are you using $pageTitle and $section without including the catalog.php where you are setting the variables?

How are you using $pageTitle and $section without including the catalog.php where you are setting the variables?

2 Answers

Mike Dion
Mike Dion
3,788 Points

It's being defined in a different file before the header is included. For example, on suggest.php (code below - couldn't paste a screenshot) the $pageTitle variable is defined just before header.php is included:

// From suggest.php
<?php
$pageTitle = "Suggest a Media Item"; ?>

include("inc/header.php");

<div class="section page">
    <h1>Suggest a Media Item</h1>
</div>

<?php include("inc/footer.php"); ?>

Often a lot of programmatic steps will be taken before including the header (setting the page title, setting meta, instantiating objects that will be used throughout the page, etc).

I used to struggle with the idea that header.php should be the first thing and was writing some pretty awful conditional statements to make that work 5 years ago. Thinking of the header as a component rather than following the visual cue from the website that it's the "top of the page" helped break some bad habits for me.

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 90,704 Points

To add to what Mike said above you want to include a reference to the variable in header.php so instead of the hard coded text, you'd echo the variable like this

<h1> <?php> echo $pageTitle <?php? </h1>

That will then pick up the variable defined at the top of on an individual php template e.g. home.php