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

Rick Banich
Rick Banich
6,846 Points

Undefined Variable

Having a problem with the Shirts 4 Mike web page, after adding the php code to include the class "on", the home page shows a white background behind the shirts link. when I click on the link and arrive at the shirts page it appears ok. upon inspection of the source code it gives me the following notice:

<li class="shirts <br /> <b>Notice</b>: Undefined variable: section in <b>C:\xampp\htdocs\inc\header.php</b> on line <b>17</b><br /> "><a href="shirts.php">Shirts</a></li>

here is the code in my header.php line 17:

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

and here is how I created and defined my $section variable on my shirts.php page:

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

11 Answers

Shen JIE LIN
Shen JIE LIN
10,612 Points

when a browser tries to load your website, it first loads the index.php file and since computer executes the code line by line in order. then by putting the

$section = "home";

before the

include(ROOT_PATH . 'inc/header.php'); ?>

is pretty much like declaring it.

because the

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

is within the header.php file I assume, and so it will be ran after

$section = "home";

this code blow

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

is within the shirts.php file I assume, and so this chunk of code is actually ran after, therefore if you did not declare the variable $section in the index.php you may get that notice.

Shen JIE LIN
Shen JIE LIN
10,612 Points

to paste code in here, for example if you want to paste php codes in here

type

"```php"

"type your code within here, without the quotation mark"

"```"

without the double quotation there

php code here
Shen JIE LIN
Shen JIE LIN
10,612 Points

about that notice, did you declare your $section variable in the index.php similar to the following?

$section = "home";
include(ROOT_PATH . 'inc/header.php'); ?>
Shen JIE LIN
Shen JIE LIN
10,612 Points

Try this on your line 17 and see if it fixes the problem

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

I noticed you did not close off your quotation mark, and tags

Rick Banich
Rick Banich
6,846 Points

Thank You Shen for getting back to me regarding this. However it seems through my initial frustration of trying to paste my existing code into the text box, by the way I would also like to thank you for the tips on how to do this, I guess I tried typing my code and forgot the quotation marks. However, my source code does include them. What I was wondering about was the notice about an undefined variable in the code.

any ideas?

Thanks Again Rick

Rick Banich
Rick Banich
6,846 Points

I should also add that I am currently watching the video "Adding Active States to the Navigation" in the course "Build a Simple PHP Application" and have been copying the code as I see it in the video. I have gone over it with the video and cannot find where I may have gone wrong.

Rick Banich
Rick Banich
6,846 Points

Hi Shen

I tried that and it worked. Thank you! I don't recall this being addressed in the video for the course, and these were downloaded files. Are you able to explain why this is needed or why it works.

Thanks Again Rick

Shen JIE LIN
Shen JIE LIN
10,612 Points

please vote a best answers within my answers, if one of them solved your problem :) and thanks.

Rick Banich
Rick Banich
6,846 Points

Thanks Again Shen

I will include your answer within my notes.

Rick

Stephen Crispini
Stephen Crispini
726 Points

I had the same issue. Added $section = "home"; to index.php as Shen suggested and if fixed it. Weird that wasn't addressed in the video.

"Shen JIE LIN" Thank you very much, your solution solved the problem, it would have been nice to have the instructor mention this in the video.