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 Adding Active States to the Navigation

What I am doing wrong?

I get the following message when I load the page: ! ) Notice: Undefined variable: section in C:\wamp\www\php\inc\header.php on line 17 Call Stack #TimeMemoryFunctionLocation 10.0010131960{main}( )..\index.php:0 20.0010134264include( 'C:\wamp\www\php\inc\header.php' )..\index.php:3 ">

Notice: Undefined variable: section in C:\wamp\www\php\inc\header.php on line 18 Call Stack #TimeMemoryFunctionLocation 10.0010131960{main}( )..\index.php:0 20.0010134264include( 'C:\wamp\www\php\inc\header.php' )..\index.php:3 ">

Code:

<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 <?php if($section == "shirts") { echo "on"; } ?>"><a href="shirts.php">Shirts</a></li>
            <li class="contact <?php if ($section == "contact") { echo "on"; } ?>"><a href="contact.php">Contact</a></li>
            <li class="cart"><a href="#">Shopping Cart</a></li>
        </ul>

    </div>

</div>

<div id="content">

6 Answers

Thanks for finally posting your code. In your index, above include, add this - $section = "home";

Great! That solved the problem. Thanks!

Master Marketing
Master Marketing
1,200 Points

You're a life saver! Thanks! c:

Master Marketing, thanks. I'm glad to save lives, haha ^_^ You're welcome.

Master Marketing
Master Marketing
1,200 Points

Lol! You're welcome! I have been going through this problem for a while now. So glad we could get a lot of help in the treehouse forums. Thanks again! ^^

Hi Mark,

Are you defining $section before including header.php on each page?

<?php
$pageTitle = "Mike's Full Catalog of Shirts";
$section = "shirts";
include('./inc/header.php');
?>

Try removing the semicolon

<?php echo $pageTitle; ?>

to

<?php echo $pageTitle ?>

If that doesn't help. Let us know.

Hello Gloria,

Unfortanly It didn't helped. I still get the same error messages on the index page. The contact and the shirts page are working fine.

Can you paste the whole code?

Blaize Pennington
Blaize Pennington
13,879 Points

It looks like your issue is you have not defined the variable $section anywhere. So PHP is trying to test your if statement, but can't find $section so it is printing out an error.

If you show me both your index.html code and your full header.php code I may be able to figure out exactly where this definition is failing.

Hello Blaize,

Index:

<?php $pageTitle = "Unique T-shirts designed by a frog"; include('inc/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('inc/footer.php'); ?>

Header:

<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 <?php if($section == "shirts") { echo "on"; } ?>"><a href="shirts.php">Shirts</a></li>
            <li class="contact <?php if ($section == "contact") { echo "on"; } ?>"><a href="contact.php">Contact</a></li>
            <li class="cart"><a href="#">Shopping Cart</a></li>
        </ul>

    </div>

</div>

<div id="content">
Blaize Pennington
Blaize Pennington
13,879 Points

You have not defined $section in your index.html file, and this is why you are getting an error. Right below $pageTitle = "Unique T-shirts designed by a frog"; type $section = "shirts";. This should fix your problem.