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 PHP Basics PHP on the Web Combining Multiple Files with Includes

nico dev
nico dev
20,364 Points

Adding classes to HTML tags in php file

Not sure if this question makes a lot of sense or my mind is totally numb today :) That's a serious possibility.

Now, when I added a paragraph to the units.php file, all worked smoothly. The next step I wanted to try, so to understand better how everything works, was the classic 'changing the background color' thing.

Now, I tried adding a class to the new paragraph within the echo statement, and then added style to that class in the CSS. When I reload the index.php, it gives me a syntax error. I removed the class, and it's alright.

For now, for a paragraph and pure testing, that's not relevant, but how do you go about it when working in actual content in an actual website?

I was thinking of alternatives: I could try using the style tags, but I don't think that's necessarily up-to-date/elegant, is it? Another way would be adding an extra div/container to it from the index.php and assigning a class, but that's extra markup, right? The last (and craziest) thing I thought is, would it be related because the css is not linked from unit.php, but from index.php, that it doesn't work. In that case, should I link to it from the unit.php file?

If this is not relevant and later I will learn why, just let me know and I'll shut up and continue learning patiently :)

Thanks in advance for your insight!

1 Answer

Hi there, Nico!

it is completely ok, to ask such dumb questions ;) What will you learn if you wont ask them?)

You can add inline styles, global styles or classes into your code. But if you add classes ensure that they exist and they included before their declaration in your code.

Hope this short example will explain few things.

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <style>
        .red, .blue {
            width: 100px;
            height: 100px;
        }
        .red {
            background-color: red;
        }
        .blue {
            background-color: blue
        }
    </style>
</head>
<body>
    <div class="red"></div>
    <div class="blue"></div>
    <?= '<div style="background-color: yellow; width: 100px; height: 100px"></div>'?>
    <?= '<div class="red"></div>'?>
</body>
</html>

Best regards.

nico dev
nico dev
20,364 Points

Hey Serhii Lihus ,

Thanks for your cool answer!

Yes, thanks to your code (I just tried it and saw it work beautifully), I noticed which was my problem earlier. You won't believe it, but man, I warned you my mind was not on its best today.

This was my code:

<?php
// some code here
echo "<p class="red_bg">Weight: ";
echo $pounds;
echo " lb = ";
echo $kilograms;
echo " kg</p>";
// some code here
?>

I don't even need to comment because my mistake was too painful and obvious, but hey, your single quotes helped me figure it out after many different attempts! :) Thank you!