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

Dainis Putans
Dainis Putans
3,487 Points

Notice: Undefined variable

I did everything as in video "Adding Active States to the Navigation", after everything I did I got message "( ! ) Notice: Undefined variable: section in". Checked my code many times, looks everything as instructor does. My browser Opera & FireFox

Mind posting your code?

5 Answers

Hi Dainis,

This thread might help you: https://teamtreehouse.com/forum/php-nonexisting-variable I gave an answer there.

We don't see it in the video because I think the instructor never goes back to the index page. It's the index page that generates the notice error.

Let me know if you have any questions about it.

Dainis Putans
Dainis Putans
3,487 Points

it's a 3 pages, the index.php requesting header.php using include();

1. page header.php 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 from Pavel</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">

2 page. shirts.php

<?php 

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

 ?>

<div class="section page">
    <h1>Pavel&rsquo;s Full Catalog Of Shirts</h1>
</div>

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

3 page. contact.php

<?php 
$pageTitle = "Contact Pavel";
$section = "contact";
include 'inc/header.php'; ?>

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

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

Okay, but how does your index.php look? Also, I'm going to go in and clean up the way your code is cited.

Dainis Putans
Dainis Putans
3,487 Points

Here is index.php, I was thinking maybe it's because the same variable name $section, but it works with instructor and message said undefined variable name

<?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="Pavel the Frog says:">
            <div class="button">
                <a href="#">
                    <h2>Hey, I&rsquo;m Pavel!</h2>
                    <p>Check Out My Shirts</p>
                </a>
            </div>
        </div>

    </div>

    <div class="section shirts latest">

        <div class="wrapper">

            <h2>Pavels&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"; ?>

Apparently, you didn't define $section in your PHP.

Dainis Putans
Dainis Putans
3,487 Points

I defined it in shirts.php & contact.php

''' <?php

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

?>

<div class="section page"> <h1>Pavel’s Full Catalog Of Shirts</h1> </div>

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

''' ''' <?php $pageTitle = "Contact Pavel"; $section = "contact"; include 'inc/header.php'; ?>

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

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

'''

Dainis Putans
Dainis Putans
3,487 Points

Thank you Jessica and big thanks to you Jason Anello. It works, I actually found similar answer on stockoverflow: http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index but your help was more easy to digest :)

You're welcome.

Yes, that's a good link. It explains a lot and also why your first choice should not be to suppress these notices/errors but try to fix them instead.