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

Adding active states to the nav

I found a problem here and have spent a good deal of time to try to solve it, I eventually decided to look in the forum and found the answer here: https://teamtreehouse.com/forum/issue-with-adding-active-states-to-the-navigation

However, I would really really like to understand why! Why does this not work unless index.php has the $section variable when it is not being used??

To ask the question why is to understand in my book :)

1 Answer

Because the $section variable is referenced within the index.php, it should be defined. If it is not defined, php will kick a undefined variable notice. If you don't want to set the variable within index.php, you can use isset() to determine if a variable is set prior to referencing it. In the reference example, the following might be a bit more appropriate considering the variable may not be declared:

<?php if (isset($section) && $section == "shirts") { echo "on"; } ?>