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 Variables

Variable undefined error

I need a bit of help here, I am new to php and I am following on the PHP deep dive.

The problem is i have declared the variable as advised in the dive.

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

on the contact.php page when trying to access it like this in the header,

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

I get an undefined variable error. Is there anything i am missing?

Thanks in advance,

Kind Regards, Joseph

11 Answers

Hi Joseph,

The problem is clear now. You have to define $pageTitle on each page. index, shirts, and contact (before you include the file, as you did on contact.php). It is only defined on contact.php, therefore only works on contact.php.

Actually, anytime you call header.php, you must define the variables it echos, if those variables are not already defined within its own file.

Let me know how you make out.

The problem is clear now. You have to define $pageTitle on each page. index, shirts, and contact. It is only defined on contact, therefore only works on contact.

Actually, anytime you call header.php, you must define the variables it echos, if those variables are not already defined within its own file.

The variable you are declaring in contact.php is not global, but the scope of it does reach whole application.

However, contact.php is not part of your file if you open index.php, or if you open shirts.php( they are not included and should not be. ). You had the variable declared the contact file only, not index or shirts. Each page should have its own title right? Each one has a unique purpose and unique title..

So three variables, or one per page.

Richmond Lauman
Richmond Lauman
28,793 Points

Hi Joseph.

Your code is correctly calling the variable you created in the first PHP block, so if you are getting an undefined variable message, then I have to ask, is the PHP block where you created the variable included on the same page where you are attempting to echo it? Could you post the code for the entire page?

Hi Joseph !

Everything looks in order here. Is this in a code challenge, or on your local machine?

If on a local machine, you would get an undefined variable if you open header.php and not contact.php in your browser.

Thanks for the response, much appreciated.

Its running on my local machine but it is based on "Build a Simple PHP Application" project on PHP deep dive.

Below is the code for 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>
<body>

    <div class="header">

        <div class="wrapper">

            <h1 class="branding-title"><a href="./">Shirts 4 Mike</a></h1>

            <ul class="nav">
                <li class="shirts"><a href="shirts.php">Shirts</a></li>
                <li class="contact"><a href="contact.php">Contact</a></li>
                <li class="cart"><a href="#">Shopping Cart</a></li>
            </ul>

        </div>

    </div>


    <div id="content">

Followed by the code for contact.php

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

<div class="section page">
    <h1>Contact Details</h1>

</div>

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

Kind Regards, Joseph

Richmond Lauman
Richmond Lauman
28,793 Points

I am stuck. I cannot see, given what you have shown, why <?php echo $pageTitle; ?> would be giving you an undefined variable message.

Just to be certain I understand the problem... is it the $pageTitle variable that is throwing the undefined variable message, or is it some other variable?

I agree with Richmond,

This error does not have to do with the code above, but probably your paths, file names, and extensions.

Longshot and probably makes no difference Try no semi-colon after the echo'd variable? My project worked without one.

Actually any page that has '''php'<?php include('includes/header.php'); ?>''' in it produces the error. It doesn't occur on contact.php itself

For example, if you can add the following code for shirts.php and try to access it, it shows the error.

php'<?php include('includes/header.php'); ?>
<div class="section page">
    <h1>Mikes&rsquo; Full Catalog of shirts</h1>
</div>

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



Perhaps let me post the code for index.php as well to complete the example code.

 ```php
<?php include('includes/header.php'); ?>

        <div class="section banner">

            <div class="wrapper">

                <img class="hero" src="img/mike-the-frog.png" alt="Mike the Frog says:">
                <div class="button">
                    <a href="#">
                        <h2>Hey, I&rsquo;m Mike!</h2>
                        <p>Check Out My Shirts</p>
                    </a>
                </div>
            </div>

        </div>

        <div class="section shirts latest">

            <div class="wrapper">

                <h2>Mike&rsquo;s Latest Shirts</h2>

                <ul class="products">
                    <li><a href="#">
                            <img src="img/shirts/shirt-108.jpg">
                            <p>View Details</p>
                        </a>
                    </li><li>
                        <a href="#">
                            <img src="img/shirts/shirt-107.jpg">
                            <p>View Details</p>
                        </a>
                    </li><li>
                        <a href="#">
                            <img src="img/shirts/shirt-106.jpg">
                            <p>View Details</p>
                        </a>
                    </li><li>
                        <a href="#">
                            <img src="img/shirts/shirt-105.jpg">
                            <p>View Details</p>
                        </a>
                    </li>                               
                </ul>

            </div>

        </div>

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

Hi Leonardo, I tried to leave out the semicolon but the error still persists.

Could this have an effect?

I have declared the pageTtile variable inside the contact.php file which is outside the includes folder and i am trying to access it from the header.php which is inside the includes folder. ie contact.php is at the root while while header.php is inside includes folder e.g, /includes/header.php

Right,

Dumb idea about the semi-colon. I was being silly.

Your doing everything right, and I just copied your code and recreated your file tree the way you described, and I have no issue.

Zip your folder and send it to me if you like:. leo@radbitt.com

Otherwise, i dunno? Start fresh?

Thanks very much Leonardo and Richmond for your responses

I think i must have misunderstood the concept of global variables in php. I thought its similar to other languages where a global variable would be visible across the whole application. So does it mean the php global variable would be scoped in a singe page/file only?