Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,111 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,111 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>";}?>

??