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 Simple PHP Application Creating the Menu and Footer Using Variables for the Title Tag

Title not displaying on local host.

Hello I am having issues getting my title to display correctly This is on my contact.php

<?php 
    $pageTitle = "Contact Mike";
    include('inc/header.php');?>

and my header.php

<html>
<head>
    <title><?php echo $pageTitle; ?></title>
    <link rel="stylesheet" href="css/style.css" type="text/css">
    <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Oswald:400,700" type="text/css">
    <link rel="shortcut icon" href="favicon.ico">
</head>

and when you render the page the title is blank in the html source

<title></title>

4 Answers

Hey Scott,

This works fine for me, but you may be using an older version of PHP.

You could try adding a variable to the global scope,

This will allow you to access the variable from any scope.

contact.php

<?php 
// Set the global variable
$GLOBALS['pageTitle'] = 'Contact Mike';
// Include header
include('inc/header.php');

inc/header.php

<?php
// Declare that we want to access the global variable
global $pageTitle;
?>
<html>
<head>
    <title><?php echo $pageTitle; ?></title>

Tidying that up

I don't recommend setting many global variables, so you could always set an array and use it for any other information that you would like to pass into other files.

global.php

<?php
// Set the global scott variable with the default values
$GLOBALS['scott'] = array(
    'pageTitle' => 'Default Title',
    'pageDescription' => 'Default Description'
);

contact.php

<?php 
// Load default global array
include('global.php');
// Update pageTitle
$GLOBALS['scott']['pageTitle'] = 'Contact Mike';
// Include header
include('inc/header.php');

inc/header.php

<?php
// Declare that we want to access the global variable
global $scott;
?>
<html>
<head>
    <title><?php echo $scott['pageTitle']; ?></title>
    <meta name="description" content="<?php echo $scott['pageDescription']; ?>">

This will give you the following output

<html>
<head>
    <title>Contact Mike</title>
    <meta name="description" content="Default Description">
Max Bailey
Max Bailey
8,245 Points

Hey Scott,

Could you post all of your contact page up to <body>?

This is my full contact.php

<?php 
    $pageTitle = "Contact Mike";
    include('inc/header.php');?>

    <div class="section page">

            <h1>Contact</h1>

    </div>

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

I have the latest MAMP stack on my Mac, and I am using Google Chrome. I cleared the cache and data and still nothing. I tried Safari, and Firefox and still get the same results. I have PHP 5.5.3 running that came with the latest MAMP stack, and the latest PHP build is 5.5.13 according to php.net. The port is set to 8888 on my apache server. The tutorial said set it to 80 but I wasnt able to get apache server running. It just stayed red when I tried to start the server. Thank you for the globals, but I would really like to find out if it is a setting in PHP or something wrong with my stack I have installed. It is important I get a good grasp on PHP, and I want to make sure I understand what I am doing wrong.

Max Bailey
Max Bailey
8,245 Points

I'm not exactly sure Scott. When I paste the same code into Mamp, it works for me. Check your directory structure maybe? Below is a screenshot of exactly what I had.

screenshot