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 PHP Basics (Retired) PHP Conditionals & Loops Foreach Loops

Xing Li
Xing Li
2,112 Points

why is there the only "}" includes in <?php ?>

why is there only a "}" (see line 52) includes in <?php ?>

3 Answers

Kevin Korte
Kevin Korte
28,149 Points

That closes the foreach loop that was opened on line 48. The PHP parser really doesn't care where you open and close php tags, it'll parse it as one when it runs. In this case, with that much HTML inside of the foreach loop, it was easier to close the php tag, write the html out, and than open a php tag just to close the loop again. The PHP parser will still loop each list item even though that code is not inside of a php tag, it's still inside of the loop.

Hope that wasn't too confusing.

Xing Li
Xing Li
2,112 Points

Does that mean everything under <li>....</li> doesn't have to be included in <? php ?> ? what if i want the whole thing (line 47 - line 53) in one <?php ?> how to rewrite the code?

Gunhoo Yoon
Gunhoo Yoon
5,027 Points

According to workspace line 47 ~ 53 is this.

 <li><a href=""><span class="icon twitter"></span></a></li>
      </ul>
    </section>
    <section class="main">
      <ul>
      <?php

You mean you want to rewrite them as pure PHP code?

Kevin Korte
Kevin Korte
28,149 Points

Does that mean everything under list item tags doesn't have to be included in <? php ?> ?

Everything that is php related would still have to be in the php tags. Remember, the php parser does not care where or when you open and close your tags, as long as they are open and closed correctly, and only php is inside of them.

what if i want the whole thing (line 47 - line 53) in one how to rewrite the code?

I would not recommend doing this. It get's too messy, to fast, but this is how it would be done.

<?php
foreach ($social_icons as $icon) {
  echo "<li><a href=''><span class='icon " . $icon . "'></span></a></li>";
}

You can fork this code if you want to play with it here: https://w.trhou.se/m5a4dq3c43

Gunhoo Yoon
Gunhoo Yoon
5,027 Points
<?php

for($counter=0; $counter <= MAX_BADGES; $counter++){
    echo "<li>" . $counter . "</li>";
}          
?>

You gotta see the whole block. for (condition...) { code_in_block.... }

The reason for leaving single closing brace in a new line is to easily detect the ending of block code. In other word, to enhance readability of code.

<?php for($counter=0; $counter <= MAX_BADGES; $counter++){echo "<li>" . $counter . "</li>";}?>

You can do this but it's hard to read.

why dont we just use <?php for($counter=0; $counter <= MAX_BADGES; $counter++){echo "<li> $counter </li>";}?>

instead of this: <?php for($counter=0; $counter <= MAX_BADGES; $counter++){echo "<li>" . $counter . "</li>";}?>

??