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 Build a Simple PHP Application Listing Inventory Items Creating the Products Array

Nino Roldan
PLUS
Nino Roldan
Courses Plus Student 9,308 Points

Going in and out of php and html too much?

Hi, I am going through this lesson and I am not sure if I feel comfortable going in and out of php and html as illustrated in the first code block below

<ul>
    <?php
        foreach ($products as $product) {?>
            <li><?php echo $product; ?>
        <?php } ?>
    </ul>

especially as the code below also displays the same thing and with only 1 call for php instance

<ul> 
    <?php
        foreach ($products as $product) {
            echo "<li>$product"; 
     } ?>

</ul>

3 Answers

Nino,

Robert does have a point and I agree with him. In large projects you will have front-end developers and back-end developers. HTML is a front-end code while PHP is a back-end code. PHP parses its code and produces HTML for the browser. In projects that contain HTML and PHP, you really want to separate the PHP that needs to be parsed and the HTML that is already there. Although it may look better and you can get the same results, if you are working with multiple people (especially people that may or may not understand PHP, it's good to surround only code that needs to be parsed by the PHP parser around the PHP tags.

How ever you want to produce your code is your own way and in large projects there will usually be coding rules when you program with others, to answer your original question, yes they will output the same, but depending on how you are coding and for whom, you may need to put your PHP code around the tags and nothing else or you may need to put the entire file around the tags. A good rule I use it if you are going to have a lot of static HTML in your file, it's better to only surround the code that needs to be parsed with the PHP tags; and if you don't have a lot of static HTML, just echo them out and surround everything with the tags. If you aren't restricted by coding rules, do it your own way.

And before you may ask this question, the code challenges do not care how you use the tags in your code ( most of the time :D ). Hope this helps.

Cheers!

Robert Walker
Robert Walker
17,146 Points

Hi Nino,

From my understanding of it, using PHP and HTML from the first example is done so that when you work on larger projects with multiple people changing the same files, people can easily read and understand what is going on. So when someone comes to change the HTML on a project they do not change PHP code.

I think this becomes more relevant when you come to larger projects and start using frameworks / MVC with lots of people working on the same project.

As I say though, this is just my understanding of it, would be nice to have an answer from a staff member as it's a good question.

Nino Roldan
PLUS
Nino Roldan
Courses Plus Student 9,308 Points

Thanks Shawn and Robert.

I realize also that the lesson I am taking now wanted to do it that way to illustrate how easy it is to mix php and html to get the desired results.