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
Matej Kovac
Courses Plus Student 1,379 PointsMingling PHP and HTML lession
Hi, I am just watching this course and it's a lot of fun, but I have one observation on there, this is actually in next video, "Creating the Products Array" When creating your list of products, you used php tags 3 times, instead of that, wouldn't it just be easier and less confusing for users to write it this way: <?php foreach($products as $product) { echo "<li>{$product}</li>"; } ?> I know your solution is better when there is a big chunk of static HTML to display, but I personally want smack someone in the head when I get short code like that, that I have to work on.
One more observation about this forum. When I write in this message text area, I can't scroll up to see what I have already wrote, which would be really useful.
3 Answers
Jah Chaisang
7,157 PointsMatej,
I believe you mean <?php foreach($products as $product) { echo $product; } ?>
Well, like you said the loop will look messy quickly when you insert more html elements. And most often you are going to display more html on a loop like this. (This code above should actually have <li> tags). I think it's a good idea to keep your for loop format consistent, big or small. I would personally smack the person who keep switching between different formats lol.
I first thought of <?php ?> tag as something that segregates a block of code from html. But once I learn other frameworks like wordpress and Ruby on Rails, I realize that when you mingle html and backend code, it's better to think of these tags, as tags. The code inside can be one line, partial line, one word, just like html tags. You should feel free to open and close them as much as you like to preserve the integrity of your code, both in terms of human coder readability and keeping it easy for future addition.
Randy Hoyt
Treehouse Guest TeacherEchoing out HTML tags with classes and IDs can get really messy. Take this code block, for example; it can be tricky to find the error here at first glance:
<?php
echo '<ul>';
foreach($products as $product) {
echo '<li class="' . $class . '><a href="/shirt.php?id=" . $product["url"] . "'>" . $product["name"] . "</a></li>";
}
echo '</ul>';
?>
In a real project it's almost always best to mingle them together like I've explained in the video, opening up new PHP tags to get access to the variables:
<ul>
<?php foreach($products as $product) { ?>
<li class="<?php echo $class; ?>"><a href="/shirt.php?id=<?php echo $product["url"]; ?>"><?php echo $product["name"]; ?></a></li>
<?php } ?>
</ul>
But I always recommend using the method that makes the code as easy as possible to read and maintain over time. When you are dealing with simple values, then putting it all in one code block might be best; but as you add HTML tags, then mingling them together like this might be best.
Does that help?
Matej Kovac
Courses Plus Student 1,379 PointsIt's actually easy to see error, but that is not my point. I wouldn't write it that way nor am I saying it should be written that way, I am saying code should be written to get best readability, because on big files things can be very ugly and they shouldn't be written in some universal way just for the sake of it. I just think, people should know there are other options. I know these are some basic things to get them started, but non the less.
I am also not saying your way is bad, I just like things to be readable, and when I saw what you wrote there... ugh... Don't get me wrong, I like this course and how you approach it. It's not boring, quiz after lessons are great to do something and repeat things. And working on project that will come to life and look good, not just some basic HTML. It is exciting and I actually added a lot of things there on my own and I have done some thing differently in code, but that's personal approach. I was member on Lynda.com, and although they have great instructors and courses, they are just so boring to watch and I always got sleepy. I am not a novice to programming, but I like to read different books and look at different courses to see different approaches and recommendations.
Randy Hoyt
Treehouse Guest TeacherIn that video, my goal was to explain how to mingle HTML and PHP code. It's easiest to see how it works with a simple example. But in the last video in this badge ("Understanding Whitespace"), I say this: "I recommend using whatever approach makes a particular block of code easier to read." I completely agree with you: sometimes consistency can makes things overly complex. :-)
Michael O'Malley
4,293 PointsI'm in the air about this subject. After perusing over some off our internal code, I noticed each and every developer seemingly has their own style. One developer is constant through and through. Although it made certain pieces of the process more complex, it was still legible. Then we have another developer that has a more dynamic approach, but almost haphazardly so. So if you're not going to remain consistent, at least be consistent with your inconsistency.
Matej Kovac
Courses Plus Student 1,379 PointsMatej Kovac
Courses Plus Student 1,379 PointsMy li isn't displayed, but it should go around {$products} I don't agree on consistency, I think priority should go to readable code, it helps not only you, but to any1 else who will use your code later. PHP unlike for example Python can get really messy, because it doesn't force you to write "beautiful code" And this are not so different in formats, since it keeps code readable. For example, my brother is great programmer (much better than me), but every time I see his code I get pissed of because it's hard for me to find my way there, but when he get's my code he doesn't have any complaints finding anything except "I would have done it differently."
I am only saying shouldn't the emphasis be on readable code, rather than something universal that can make code messy.