-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

10
  • I never heard of that, furthermore your browser doesn't see any of the PHP variables. Can you provide some code? Commented Jan 30, 2014 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. Commented Jan 30, 2014 at 14:27
  • There is no correlation between CSS and PHP. There must be something else going on. Commented Jan 30, 2014 at 14:28
  • PHP is server-side. CSS is client-side. No interaction. You're looking in the wrong place. Commented Jan 30, 2014 at 14:29
  • 2
    Why so many down votes for this poor guy who just missed his own css styling.. Commented Jan 30, 2014 at 14:38

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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.

3 Comments

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.
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..?
Hehe I did not notice. Thanks once again :)
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.

7 Comments

That isn't what is causing his issue, though he will need to take a look.
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
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.
Your answer is exactly what I say ... whatever ... go get your points. :D
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.