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 trialKamil Doležal
1,696 PointsWhy cant i just echo the html elements instead of using multiple <?php?> ??
Well, i guess theres a reason why youre doing things this way, so can you please explain to me why? Kinda curious :P.
2 Answers
Jayden Spring
8,625 PointsI haven't done this course, but have been using PHP for years. Basic PHP where you are including HTML inline can get quite messy with multiple blocks of code as I guess you are using.
If you have this (for example)
<?php
$arr = array( "key1" => "val1"
"key2" => "val2"
"key2" => "val2");
foreach($arr as $k => $v)
echo "<li class=\"$k\">$v</li>";
//Output would be <li class="key1">val1</li> etc
?>
</ul>
If you were writing a fully fledged web app mixing your logic with your controllers and views would get extremely messy! Now you could start including or requiring various templates/files to tidy code up (I would believe that TH go over that in the e-commerce application).
I would suggest at some point using a template engine such as Smarty, or use an MVC framework of which there are many out there.
Emmanuel Salom
Courses Plus Student 8,320 PointsI think this course was an introduccion to php and creating a web app. Remember that not all members in treehouse are as experienced as you. The course is a starting point for most users. Just my 2cents
Jayden Spring
8,625 PointsI understand that completely, I still have my first project I ever wrote in PHP. Its long and hurts my eyes!