-1

I'm working on an e-commerce site as part of an online PHP class at Treehouse.com. I ran into an issue where one of the menu items was displaying incorrectly as can be seen here: http://herkuhleez.com/shirts4mike/

Upon looking for an answer in their forums, another student concluded it was an issue with the newer versions of PHP as the older versions had no display issues with the exact same code as seen here: http://cheetahcandy.com/shirts4mike/

The first example (with the error) is running PHP version 5.4.24 and the second one is running 5.2.17. My localhost server is running 5.5.6 and I have the same error as the first example.

This student however discovered a work around in newer versions by simply changing any php variable names that are the same as the css selector names, at least when the php is written inside a tag's class attribute.

My QUESTION is: Is this a bug in these newer versions of PHP or is this working as intended as some sort of auto-correct function?

EDIT* Here is the forum discussion over at Treehouse: https://teamtreehouse.com/forum/build-a-simple-php-application-adding-active-states-to-the-navigation-php-versions

Chad Pjontek
  • 303
  • 1
  • 7
  • I never heard of that, furthermore your browser doesn't see any of the PHP variables. Can you provide some code? – Lars Beck Jan 30 '14 at 14:26
  • I've read your question 4 times and I'm still clueless as to what your asking or what the problem is. PHP has no idea about CSS and vice versa. – Mike B Jan 30 '14 at 14:27
  • There is no correlation between CSS and PHP. There must be something else going on. – Styphon Jan 30 '14 at 14:28
  • PHP is server-side. CSS is client-side. No interaction. You're looking in the wrong place. – Ryan Jan 30 '14 at 14:29
  • 2
    Why so many down votes for this poor guy who just missed his own css styling.. – Damien Jan 30 '14 at 14:38
  • Don't know why this got so many downvotes. It's a very obvious problem, but we were all learning once. – Styphon Jan 30 '14 at 14:38
  • Sorry I added a link to the forum discussion at Treehouse that has a bit more insight into the situation along with some code but I don't have access to my comp atm. Will update on this later. – Chad Pjontek Jan 30 '14 at 15:02
  • I have updated my answer for you. This is not a bug, it is merely a case of victim of circumstance. See my answer for an explanation of this. If I had an account on that forum you posted, I would offer my answer there instead :) – Damien Jan 30 '14 at 16:17
  • That forum post is loaded with misinformation. The moderator's post hardly addresses any of these points. Case of the blind leading the blind. – Mike B Jan 30 '14 at 16:25
  • I agree - I have notified the moderator there, but they have not responded. I am more concerned about future newbie devs running into some kind of issue and through searching end up landing over there - then believe what they're reading. Unfortunately, unless you pay, you cannot comment over there. – Damien Jan 31 '14 at 05:30

3 Answers3

2

If you have a look at your CSS on the site with the error, you will notice that your class .section.shirts has the following css on line 605 of style.css

padding-bottom: 42px;
background: #fff

Funnily enough if you take both of those out, it looks exactly the same as the other website.

In addition, the class also has an error in it:

shirts <br /> <b>Notice</b>: Undefined variable: section in <b>/home4/herkuhle/public_html/shirts4mike/inc/header.php</b> on line <b>17</b><br /> 

I would have a look at header.php on line 17 to see what is causing that.

Styphon
  • 10,304
  • 9
  • 52
  • 86
  • Whilst the style is not helping the situation, it is definitely not the problem here. The style is present on the other example site, it is the presence of the word created by the error `section` inside double quotes within a class as a result of the error that is causing it to be applied. That style may be used somewhere else for an entirely different purpose, best not to change it and just fix the PHP error. – Damien Jan 30 '14 at 15:21
1

Just have a look in your HTML markup and you 'll recognize the following php notice:

<b>Notice</b>:  Undefined variable: section in <b>/home4/herkuhle/public_html/shirts4mike/inc/header.php</b> on line <b>17</b>

Just have a look in your header.php file around line 17. Your problem is a follow up of this php notice. Looks like a css class isn 't defined properly.

Marcel
  • 4,854
  • 1
  • 14
  • 24
  • That isn't what is causing his issue, though he will need to take a look. – Styphon Jan 30 '14 at 14:34
  • To answer his question correctly: No, this is NOT a bug of the newer PHP version. To answer in a more detailed way I have to know the PHP code from the header.php file, which is not given here. So yes ... he has to take a look for himself. *lol* – Marcel Jan 30 '14 at 14:36
  • Actually his issue was pretty obvious if you took 2 seconds to inspect the elements on the page. Check out my answer if you want to know. – Styphon Jan 30 '14 at 14:37
  • Your answer is exactly what I say ... whatever ... go get your points. :D – Marcel Jan 30 '14 at 14:39
  • No, it's not what you say, not once do you mention CSS. You said his problem is PHP, in the header file, which whilst there is a problem there, that's not what is causing the visual error he asked about. – Styphon Jan 30 '14 at 14:40
  • Sure ... if he would fix the php notice, the .section css class would be added like in the version on the older php environment. It 's a problem of unclean coded php not a css problem. But as I said before ... whatever dude. – Marcel Jan 30 '14 at 14:43
  • you don't understand the problem, even with the PHP notice the .section css class ***is*** being added, that is the problem. He has custom styling that needs removing. It's a CSS issue, **not** a PHP issue. And as I said before, if you took 2 seconds to inspect the elements of the page you would see that. – Styphon Jan 30 '14 at 14:44
1

MORE INFORMATION

EDIT: This question seems to relate to a predefined project available at an educational site. The OP seems to be confused as to why the exact same code fails on some servers, and seems to work others. They were thinking it was something to do with the PHP version. It is more likely as a result of the suppression of warnings server side. To suppress warnings you could simply add:

error_reporting(E_ERROR | E_PARSE);

However; it would be far better to define or test the variable properly [in this case $section] and not simply mask the issue.

Using a PHP variable called $section along with a CSS selector of the same name will work

..Just because you can do something, doesn't mean you should..

Anyway, it is not a CSS error per se, but it certainly doesn't help in this circumstance.

Take note that the error/warning in your code has the word section in it:

<li class="shirts <br /> <b>Notice</b>: Undefined variable: section ....."> 

Both examples have a CSS style called section.shirts:

.section.shirts {
    background: none repeat scroll 0 0 #FFFFFF;
    padding-bottom: 42px;
}

As a result of the placement of the error/warning, most browsers will interpret your class as to include any of the words found within the quotes (eg; shirts undefined section etc) and apply them all as styles if found in your CSS. In your version of the page, as a result of the placement of the error, you are applying the styling above. The other site does not have an error/warning with the word section within double quotes inside a class so it does not apply that particular styling.


There is no correlation between PHP variables and CSS selectors (read: There is no conflict between PHP Variables and CSS Selector Names/variables in current, older or dare I say it, future versions of PHP) with the same name unless you generate an error in your resultant HTML in an unfortunate position. In this case, you have done this by creating an error between double quotes inside a class.

Fix your error and your styling will apply as expected. No bug here (other than line 17 in your code :)

Without seeing your code, to fix your error/warning:

  1. [Unlikely] Maybe add a $ in front of section where applicable in your code if it is missing somewhere (like this $section)

  2. Maybe ensure you are defining $section before you test it in your if statements

  3. Maybe Use isset when testing $section if you don't know if it was previously initialized.

So in summary, the word section is rendered inside your HTML within an error/warning and interpreted as part of your CSS class structure as discussed.

Why does it work on older versions of PHP?

If you are wondering why it works in other environments, have you considered that maybe it has nothing to do with the PHP Version - Perhaps in other environments with the same error/warning, they have configured PHP to suppress the display of errors and or warnings.

Without the error, the word section would not appear inside your class; ergo - no style issue.

From the PHP Manual:

Relying on the default value of an uninitialized variable is problematic in the case of including one file into another which uses the same variable name. It is also a major security risk with register_globals turned on. E_NOTICE level error is issued in case of working with uninitialized variables, however not in the case of appending elements to the uninitialized array. isset() language construct can be used to detect if a variable has been already initialized.

For more information about your particular error, see this previous answer.

Community
  • 1
  • 1
Damien
  • 1,490
  • 1
  • 8
  • 17
  • Thank you Damien. Upon inspection, the code did not explicitly initialize the `$section` variable. I am very new to PHP and your explanation was spot on and I am wiser for it. Thanks again. – Chad Pjontek Jan 31 '14 at 02:25
  • I don't know if you've noticed, but in the extra credit section of that tutorial (section below the video): Create a new PHP file for the page, making sure to set the $section variable. - I don't know if that was always there..? – Damien Jan 31 '14 at 04:17
  • Hehe I did not notice. Thanks once again :) – Chad Pjontek Jan 31 '14 at 13:45