Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Build a Basic PHP Website Building a Media Library in PHP Using Variables for the Title Tag

Will Albertsen
Will Albertsen
13,155 Points

variable $pageTitle undefined

For some reason, the variable $pageTitle is returning undefined up in the tab. No idea why. Here is the code.

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'); ?>

header.php

<head>
    <title><?php echo $pageTitle; ?></title>
    <link rel="stylesheet" href="css/style.css" type="text/css">
</head>

Here is what it says up in the tab when I run this:

<br /> <b>Notice</b>: Undefined variable: pageTitle in <b>/home/...

That's all I could read

4 Answers

1) Are you sure you aren't looking at index.php but if so that you have assigned a value to $pageTitle before include("inc/header.php"); in index.php?

2) If that is all of your code for header.php you can't navigate to suggest.php from the start page since the links are included in the header. However you can add /suggest.php to the address bar to load the suggest page. For example mine looks like this:

http://port-80-0zodslm0uo.treehouse-app.com/suggest.php

Doing so you'll notice the tab displays "Suggest a Media Item" as you have in the code.

Undefined here too. Totally follows the video and get same problem. Don't know how to fix.

Will Albertsen
Will Albertsen
13,155 Points

Thank you so much for your response. I have since resolved the issue although I never knew figured out exactly what the problem was :) I'm sure I probably misread some of that code when I pasted it in.

Exact same issue here.. did something change in PHP over the last 4 years? I can't be any more exact, and I even tried to tinker with it. It's just not taking into account the variable from the header on index. Welp.. moving on anyway. Maybe it'll clear up along the way.