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 trialZaberca David
5,361 PointsPHP non-existing variable
I found out that if the variable $section doesn't exist , it considers if( $section == "shirts" ) true. Is that right?
4 Answers
Jason Anello
Courses Plus Student 94,610 PointsHi Zaberca,
It doesn't consider the condition to be true. What it does do is generate a Notice Error.because the $section variable is undefined on the home page and so when those scripts run you're trying to use an undefined variable.
PHP puts out a notice to warn you about what you're doing. That notice is placed in the markup right where that script is which is right inside your class attribute. Each part of that error message is then going to be interpreted by the browser as a class name. So whichever class names happen to match up with your stylesheet is going to cause unintended styles on those links.
Here's what one of the Notices looks like:
<br /> <b>Notice</b>: Undefined variable: section in <b>C:\xampp\htdocs\shirts4mike\inc\header.php</b> on line <b>17</b><br />
Towards the end of that error message you'll see the word "on" and since that class name means we should underline the links that's why we get both links underlined.
My recommendation for fixing it is to first check if the variable is set first before trying to use it.
<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>
In my opinion this is a good solution if you have a variable that isn't always going to be used.
I don't believe the video shows this but the project files show another way to fix it. it's posslble the problem was discovered after the video shoot and it was fixed in the project files. In the project files you'll see that they set $section="home"
on the index page. I don't think it was shown in the video but I could be wrong.
This fix will work too as long as you remember to do that. If you create another page on your site that isn't going to need the $section variable, you still have to remember to set it to something or you'll get the notice on that page.
Jeff Busch
19,287 PointsHi Zaberca,
The variable was created in the shirts.php file at minute 1:06 and then "called" in the header.php file (line 17) when you click on shirts.
<?php
//**header.php** file (line 17)
<li class="shirts <?php if ($section == "shirts") { echo "on"; } ?>"><a href="shirts.php">Shirts</a></li>
//shirts.php
$pageTitle = "Mike's Full Catalog of Shirts";
$section = "shirts";
include('inc/header.php'); ?>
<div class="section page">
<h1>Mike’s Full Catalog of Shirts</h1>
</div>
?>
Jeff
Zaberca David
5,361 PointsIt works all right in shirts.php and contact.php . In index.php it underlines both buttons. Why is that happening?
Jeff Busch
19,287 PointsWould have to see your code. I'm viewing the pages with the final Project Files and everything seems to work correctly. In fact, that's what you should do. Download the files and compare them to the ones you have created, you'll learn that way.