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 trialDaniel Alarcon
8,019 PointsShirt Catalog - PHP Foreach loop
Hi !
In the Shirt Catalog exercise, why was this code used:
<code> <h1>Mikes full catalog of shirts </h1>
<ul>
<?php foreach ($products as $product) { ?>
<li><?php echo $product; ?></li>
<?php } ?>
</ul>
</code>
Instead of somelthing like this :
<code> <h1>Mikes full catalog of shirts </h1
<ul>
<?php foreach ($products as $product) {
echo "<li>" . $product; . "</li>";
} ?>
</ul>
</code>
Even though I'm not new to PHP and have seen this before, I still think that using ending PHP codes like this (the first example) is very confusing, especially when trying to find out what went wrong in the code. I can understand that this might be better than constantly echoing HTML code and trying to escape characters constantly
Any thoughts on this ?
Thanks!
4 Answers
Shawn Gregory
Courses Plus Student 40,672 PointsDaniel,
To add, I agree that both sets of code work. However, there is a huge difference between the first code example and the second. In most cases in the real world, you will have different teams working on the website: one for front-end development and one for back-end development. In the first example, there is a clear sign of separation between front-end development and back-end development.
Front-end development deals with HTML, CSS, and JavaScript.
Back-end development deals with PHP and MySQL (in this case).
In the second code example, you are displaying front-end code in your back-end code which, in most cases, is a not recommended. You want to separate visual design from program logic. In the end, you can give the code in the first example to a designer that knows nothing about PHP and allow them to design the web site while the second example would require the front-end designer to know a little bit about PHP since the visual design is coded into the PHP back-end logic. I hope I've explained myself accurately to you.
Cheers!
Steven Griffiths
3,165 PointsWell if he's not going to thank you then I will!
Thanks!
Gareth Borcherds
9,372 PointsA lot of it is code style and preference. I like doing it the first way from time to time just to make sure my html is valid. Additionally, my text editor which is Coda ends up recognizing the text better and allows me to keep things cleaner for the way I code. There really is no difference between the two in my mind, just preference. Any speed enhancements you might get from this would be minimal, but I'm not an expert on that part of php.
Mitsuharu Enatsu
10,856 PointsI was just about to ask the same question, but I checked to see first if someone asked it already. Thank you very much for the answer!