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

Nick Zachary
Nick Zachary
6,816 Points

Build a Simple PHP Application Listing Inventory Items More Excitement With Arrays QUESTION 5 of 7 Don't Understand!

I attained this answer by just copying the answer from the video. I need help to understand why it worked. Between the stars is where i don't understand.

<?php foreach($flavors as $flavor){ ?> //Why is that closing php tag not in between the "("

and "{"- Why is this important?

<li><?php echo $flavor; ?></li>
**<?php } ?>**                                             //Why does this last php syntax exist, what does it to do, and why is the "}" inside it?

Thanks for the help, I'm very confused.

4 Answers

J.T. Gralka
J.T. Gralka
20,126 Points

Hey Nick,

Take a look at this example below:

<?php
   $a = 3;
   if($a > 0) { ?>
   <h1>The value of the variable is greater than zero!</h1>
<?php } ?>

The above code is the same thing as saying:

<?php
   $a = 3;
   if($a > 0) {
      echo "<h1>The value of the variable is greater than zero!</h1>";
    } ?>

Notice that in the first block of code there are two <?php ?> segments. The first sets the value of $a and begins the conditional check to see if $a is greater than 0. The second closes the body of the if statement; in other words, if the condition is true -- which it is; three is greater than zero -- then the HTML will be passed from the server to the user's web browser and the page will display a level one header tag.

This is as opposed to the second block of code where everything takes place in one giant <?php ?> segment. Notice that I actually echo back the html to the user.

The confusion comes from the fact that in the first example, syntactically speaking, if we start an if statement and encapsulate the body of the conditional with curly braces, then it stands to reason that we must end the body with a curly brace. The first block of code introduces the opening curly brace, and the second one completes the code block with a closing curly brace.

Remember that PHP is a server side language; all of the PHP gets processed by the server before the HTML is sent to the client (the user's Web browser). So, again, the first example demonstrates how to have PHP perform an if statement and return HTML to the client if the condition is true; the second example demonstrates how to use PHP to write one PHP block and echo or print back the HTML as a string of text (which is interpreted, either way, by the browser as HTML).

I know! It is a little confusing to get the hang of at first, and it looks as if there're dangling curly braces all over the place. But, the more you work with PHP the more helpful you'll find this goofy little trick! I know Randy has said that he plans on clarifying this a little bit better in future revisions to his PHP course. You might find this earlier post a bit helpful as well!

Hopefully my explanation cleared things up for you!

Best wishes,

J.T.

[[ed. note]] Added markdown for syntax highlighting

Nick Zachary
Nick Zachary
6,816 Points

I just came back from dinner, and was pleasantly surprise. I'm very thankful for your expertise J.T. you've really helped me understand this =).

Sincerely, -Nick

Nick Zachary
Nick Zachary
6,816 Points

should sticky this post btw

James Barnett
James Barnett
39,199 Points

+1 For adding this as a featured post