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 Adding a Contact Form Forms and Post Variables

Undefined variable: section in C:\xampp\htdocs\inc\header.php on line18

I get a white box around in my header; to be exact is around the shirts Icon. Here's my code:

    <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">

    <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>



    <div id="content">

3 Answers

Hi Jose

Have you declared the $section variable before including the header file ??

for example if you include the header in the shirts listing page code should look like this

$title="shirts for mike";

the reason your getting the error is because your trying to check the $section variable without it ever being set assuming that either you have not set $section variable or you are including the header.php before the $section variable gets set.

Hi Jose,

I think you're only having this problem on the home page correct?

You either have to set the $section variable to something on all pages regardless of whether you're using it or not, as Andreas mentioned, or you can update the nav scripts to first check if the variable is set first.

<li class="shirts<?php if ( isset($section) && $section == "shirts") { echo " on"; } ?>"><a href="shirts.php">Shirts</a></li>
<li class="contact<?php if ( isset($section) && $section == "contact") { echo " on"; } ?>"><a href="contact.php">Contact</a></li>

Then you don't have to worry about setting it to something on all pages.

Okay but, I haven't really gone over the attribute isset($section) on the tutorials yet. I'm going on post variables. can you explain what does that attribute do?

Hi Jose,

I don't think it was covered up to this point.

You can read more about isset here: http://php.net/manual/en/function.isset.php

Basically, it will return TRUE if the variable has been set to some value or FALSE otherwise.

The reason you get the problem on the home page is because when you run this code if ($section == "shirts") {echo "on" ; } php will issue a notice error to indicate that the $section variable is undefined. This produces the unwanted styling on the home page.

This doesn't happen on the "shirts" or "contact" page because you do set the $section variable to either "shirts" or "contact" on those pages. Then you don't get the notice error.

By updating the code to check first if the variable has been set before you do the comparison then you can avoid getting that notice error on the home page.

You could put $section = "home"; on your index.php page to solve the problem but you have to remember to do that on all pages. In this case we only have a couple of pages to worry about but a real site could contain a lot of pages.