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

General Discussion

Danielle Quevedo
Danielle Quevedo
17,935 Points

Build a Simple PHP Application > Adding Active States to the Navigation - PHP versions

Build a Simple PHP Application > Adding Active States to the Navigation

I'm not looking for help -- just curious if anyone else encountered this problem that I have since corrected.

The Problem

After completing the video's activity, I noticed that although the underlines appear correctly under the menu links for Shirts and Contact, when you visit the homepage, the <li> tag for the Shirts link now has a white background and an oversized 42px bottom margin. See http://herkuhleez.com/shirts4mike/

Is PHP Version The Cause?

Please correct me if I'm wrong but I believe the reason is the php version being run by the server. I have two websites in addition to my localhost Mamp server, and all three have different php versions.

http://cheetahcandy.com/shirts4mike/ has php version 5.2.17 http://herkuhleez.com/shirts4mike/ has php version 5.4.24 and my localhost server has php version 5.5.3

All files for all 3 sites are 100% exactly the same, just copied from one location to the next. The problem only appears on herkuhleez and localhost, not cheetahcandy.

Using Chrome developer tools, I found that a CSS selector called .section.shirts gets applied to the <li> tag in question for both herkuhleez and localhost. For this reason, I believe that in the newer versions of php, the php $section variable is somehow being interpreted as the CSS class .section.

My Code

My original code snippets are at the end of this post. I went over it multiple times and typed and retyped it to be certain. I also compared everything in Chrome, Safari, and Firefox. Remember, the code works perfectly on the server running the oldest version of PHP

How To Fix This

Changing the variable from $section to $sections (plural) corrects the problem. However, I found I still had to add $sections = "home"; to the index.php file so that the Shirts and Contact links would not be underlined on the homepage. After making these changes for both the herkuhleez and localhost versions, they work perfectly. See the revised herkuhleez site at http://herkuhleez.com/pants4mike/

Lesson Learned

If I'm correct, then it's best to avoid using php variable names that are the same as css selector names, at least when the php is written inside a tag's class attribute.

    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") { echo "on"; } ?>"><a href="contact.php">Contact</a></li>

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

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

13 Answers

Chad Pjontek
Chad Pjontek
13,700 Points

I noticed this same issue while working on this yesterday and did a search on the forums here and found your solution. Your explanation seems to make sense although I still don't quite grasp why it is so. Doing a Google search on "CSS section" I could only find references to the HTML5 tag section. Possibly the older versions of php don't utilize these tags? Could that be the culprit?

P.S. I did change all the instances of the variable $section to $sections in my code and it fixed the problem. Thank you :)

Danielle Quevedo
Danielle Quevedo
17,935 Points

You're quite welcome, I'm glad to help. The reason you couldn't find anything regarding "CSS section" is because ".section" was just an arbitrary name chosen for this website's CSS file to reference the HTML < div > tags that were assigned that word. It could just as easily have been ".apple" or ".football". If it had been "section", however, without the period, then the CSS code would be referencing the actual HTML tag < section >, which isn't used in the Shirts 4 Mike website. I don't think the problem is related to the HTML < section > tag though.

I think the problem lies with PHP and CSS, not HTML. Specifically, I believe the newer PHP versions are interpreting a PHP variable (which in this case just happens to be called $section) as a CSS class (which just happens to be called .section). And I believe PHP is doing this simply because that PHP variable is being placed in the same area where a CSS class reference normally goes (inside an opening HTML tag).

To test this theory, I renamed every instance of the PHP variable $section as $peanutButter. And then I renamed every instance of the CSS style .section as .peanutButter and the problem persists. Apparently, as long as the PHP variable and the CSS style bear the same name, the newer versions of PHP will get them mixed up.

So I don't think it's related to the HTML tag < section >, but kudos for considering that-- I completely forgot about that tag and hadn't originally considered it. And actually, if the < section > tag had been used in the website instead of < div >'s, the problem would not have occurred because the CSS could then reference the < section > tags directly without needing a class called .section. But the benefit of using < div > instead of < section > is that older browsers that don't use HTML5 can still view the webpage.

Chad Pjontek
Chad Pjontek
13,700 Points

Thank you for the in depth explanation. That really cleared it up for me. Very keen and diligent of you to figure that out! I wonder why however that the newer versions of PHP do this? Working as intended to auto-correct you think? Or possibly a bug? Sorry I'll stop picking your brain lol. Thanks again for your help :D

Danielle Quevedo
Danielle Quevedo
17,935 Points

Lol, no worries, and thank you. Yeah, I don't know why the newer version would behave that way. I guess it's just one of those weird things. Because of it, I'll make sure my PHP variables and CSS class names never use the same word.

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

I wouldn't think the PHP version would matter. I suspect the issue is that one server is set to display error messages in the page while the other is not. I'm curious: does leaving the variable named $section but adding ...

$section = "home";

... to the home page work properly? I suspect it would.

Danielle Quevedo
Danielle Quevedo
17,935 Points

Your suspicion is correct! Thank you, Randy!

So if this is related to an error messages display setting in PHP, and not the version, how come I never saw an actual "error message" but instead the page didn't render correctly?

Is that how this display setting is supposed to work? And if so, is the benefit of that setting that it just makes you code more strictly after you see something isn't working?

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

If you view the source, I believe you'll see the error message. It's tucked away inside an HTML class, something like this:

class="<b>Undefined variable ..."

You wouldn't normally see a class name displayed on the page in the browser, so you won't see this one either.

Does that make sense?

Danielle Quevedo
Danielle Quevedo
17,935 Points

I think so. I do see the "undefined variable" statement when I view the source code. Is that typically how all errors are viewed for the "display error messages" setting?

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

Yes. PHP will spit out the error wherever it occurs. It's meant to be displayed as HTML, and you can usually see it in the browser. But in this case, it gets spit out in a place where you can't see it in the page: inside an HTML attribute.

As far as best practices go, you will usually see development environments set to display these errors so that you can address them while you are building a site. You typically will not want them to appear on live websites, though; these are for developers to see, not for users or site visitors.

Danielle Quevedo
Danielle Quevedo
17,935 Points

oh, I see! That makes perfect sense! Thanks Randy!

Chad Pjontek
Chad Pjontek
13,700 Points

After asking around and doing a bit of research I discovered the probable reason why you were able to get it to work on the one version and not the others: Your server running PHP version 5.2.17 is more than likely set up to suppress the error message instead of outputting it. This is the full error:

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

The word section is in this HTML error code and causing the the CSS class .section to try and execute. But the reason this error code was being created was because the PHP variable $section was not initialized on the home page. That is why Randy's fix worked and the renaming of $section was not necessary.

So... if your PHP 5.2.17 server is not outputting the error then it does not try to execute the .section class in a wonky spot (aka the error message). The error is still there. It's just not showing it.

Lesson learned here: Always initialize or test your variables before using them :)

Danielle Quevedo
Danielle Quevedo
17,935 Points

Awesome. Excellent explanation. Thanks Chad!

Christopher Andrew Kemur
Christopher Andrew Kemur
7,709 Points

Thank You, i was having the same problem and this thread helps!