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

Csaba Koles
Csaba Koles
4,644 Points

Undefined Variable, $section is not working in header.php

UPDATE: Solution was found!

See the best answer on this page:

https://teamtreehouse.com/forum/so-i-did-the-tutorial-correctly-but-when-i-go-back-to-the-index-page-the-links-are-both-highlighted-underlined

Hi all,

I can't seem to make this code working. I have replaced the condition of the "if" statement with "true" and it was working without any error. Yet when I enter $section I get the message:

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

Here is my code from header.php

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

Here is my shirts.php

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

Here is my contact.php:

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

Please help, I just can't seem to spot the issue.

Csaba Koles
Csaba Koles
4,644 Points

Although I can't see it here, I received the following answer from: Andreas Nikolidakis

"Hi Csaba,

the variable section is not available in header.php because you didn't include the files shirts.php and contact.php into header.php, so $section is undefined in header.php. You did the opposite, you included header.php into shirts.php and into contact.php.

I hope I helped a little,

Andreas"

First, thank you for the answer, but it didn't work for some reason. The idea is based on the video to have a constant header that I can dynamical change so I can change only the header and all over the site the header changes hence the includes. if I change them around I get in a loop and php throws an error.

I also watched the video again and there is no include in the header.php only in shirts and contact.

I'm puzzled...

2 Answers

Ali Davutoglu
Ali Davutoglu
311 Points

Check your index page. Is $section variable defined in the index page just like shirt.php and contact.php? the Header.php looks for $section variable in while index.php is loaded, cannot find it, server shows error in the html code and page style is weird. All you need to do is, add $section variable to the index page and give a value just like you did for shirts.php and contact.php. There rest is just like in the class.

Csaba Koles
Csaba Koles
4,644 Points

Hi Ali,

You were right I just added,

$section = "home";

and the styling is back to normal!

Thanks!