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

Using multiple <?php ?> strings when listing foreach List Items

Maybe an obscure question, regarding the video "Creating the Products Array" at 3:15

So far in learning php we use a single opening and closing php bracket when writing the code. However, now that we're using a foreach command to list in an <li> we've starting using two php brackets, the second only for showing where the closing } is for the foreach loop. Looks like this:

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

So why can't we instead write it using a single opening and closing php bracket, like this:

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

Randy just did the extra bit of php without stating why it was needed to be done like that. Soo, why does it need to be done like that? :)

5 Answers

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

Great question. That's one thing I want to go back and clarify: sorry for the confusion!

The code inside the PHP tags is treated as PHP code that gets processed by the server. The code outside the PHP tags is treated as HTML that gets sent down to the browser. You can do things like this:

<?php

    $name = "Randy";

?>
<h1>My name is <?php echo $name; ?></h1>

We need the two sets of PHP tags because we have two separate blocks of PHP code mingled inside our regular HTML tags.

Take this code for example, with curly brackets around the code inside the foreach loop:

<?php

    foreach($products as $product) {
        echo $product["name"];
    }
?>

If we want to wrap HTML tags around the name, we can end the PHP code after the first curly bracket, display some regular HTML, add some more PHP code to output the variable value, display some other regular HTML, and then add some more PHP code to close the foreach loop:

<?php

    foreach($products as $product) {

?>
        <h1><?php echo $product["name"]; ?></h1>
<?php

    }

?>

Does that help?

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

I did mention it, but I've gotten plenty of feedback that it's just not clear enough. I'm working to add a further note about it somewhere. Glad this thread helped it click! :~)

This was so helpful! Essentially HTML elements within PHP tags won't be processed as HTML but rather as PHP. In order to display our HTML within a foreach loop we must close the first php block after the opening curly brace, sprinkle in our html and whatever else we'd like and then write another PHP block with the closing curly brace to signify the end of the foreach loop.

Ah that completely clicked. If you leave the HTML code within the php tags, then the HTML will be read as php and will never be rendered. So we have to pepper our php in just exactly where we need it around the HTML.

I actually think you touched on this at the beginning of PHP and I lost it along the way. Thanks, Randy!

James Barnett
James Barnett
39,199 Points

@Randy - I think you are the Treehouse teacher that is the most responsive to updating your course's based on feedback.

You get a :star:

Keep on being awesome :smiley: