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

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,735 Points

I'm confused about php script tags being opened and closed within a loop

I'm not following the logic of how this works. If the php script tags are were the magic happens, how can there be a closing tag within php code?

<ul class=“social”>
    <?php
        foreach($social_icons as $icon) {
    ?>
        <li><a href=“”><span class=“icon <?php echo $icon ?>”</span></a></li> 
    <?php   
        }
    ?>
</ul>

6 Answers

Kevin Korte
Kevin Korte
28,148 Points

Sorry I'm rambling but I think I might have answered my own question. So javascript lives inside of an html file. But in this case, it's not a piece of php inside an html file, it's a .php file. It's like the whole thing is a script, and the php tags inside of it are just places where some code is getting run instead of plain html. But a closing tag doesn't stop the php the whole thing is php. Am I on the right track?

You are on the right track. Closing PHP tags do not stop the script, it simply tells the php parser what to read, and what not to read.

You can technically open and close as many php tags as needed in a file, and as long as it's valid PHP inside the tags. You can only have HTML inside a php tag if you are echoing it inside variable. That can get messy, so in this case it is easier to close the php tag to be able to write html. In your example, even though the list item html is outside of the php tags, it's still technically inside the for each loop, so it's going to be printed as many times as the for each loop runs. The php parser isn't going to evaluate the html, but it knows it's there.

It's a good question, it get's asked many times.

Sue Dough
Sue Dough
35,800 Points

Hi Brendan there is multiple ways to use PHP tags. This is confusing at first when you see this but its a totally valid way to write PHP and can be helpful if you want to display HTML.

Hopefully this will make sense to you. Hello World will show the first time but not second time.

    <?php
    // notice the {   This keeps it open.  We are going to test if this is true
        if (2 + 2 == 4) {
            //  its true so we can move on and keep the php tag open this way with {
    ?>

        <h1><?php echo 'hello world'; // echo hello world here only if 2 + 2 = 4 ?></h1>

    <?php

        } // now we can close all of this with }
    ?>





    <?php
        // notice the {   This keeps it open.  We are going to test if this is true
        if (2 + 2 == 5) {
            //  its false!!!!!!!!  Lets see what happens now {
    ?>

        <h1><?php echo 'hello world'; // THIS WILL NOT SHOW BECAUSE 2 + 2 does not = 5 ?></h1>

    <?php

        } // hope that makes more sense : }
    ?>
  • Note that Treehouse syntax editor can't show this right but if you put it in a good text editor for php it is valid.
Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,735 Points

I believe you that it works. I think I just have some faulty assumptions about what these tags are doing. In my head, the closing ?> tag says "end of the script", but it's not over yet. And why is it necessary to close the php tag before putting in an html tag? I guess I'm trying to figure out how this is different from a javascript script.

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,735 Points

Sorry I'm rambling but I think I might have answered my own question. So javascript lives inside of an html file. But in this case, it's not a piece of php inside an html file, it's a .php file. It's like the whole thing is a script, and the php tags inside of it are just places where some code is getting run instead of plain html. But a closing tag doesn't stop the php the whole thing is php. Am I on the right track?

Sue Dough
Sue Dough
35,800 Points

PHP can't be in a html file. PHP needs to be in a PHP file. Html can be in a PHP file. Javascript can also be in a PHP file.

For example Hello world will show 3 times. Once in PHP. Once in HTML. Once in Javascript.

This file is going to be called hello.php

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Hello World</title>
  </head>
  <body>

    <?php echo "hello world";?>

    <h1> Hello World </h1>

    <script> document.write('hello world'); </script>

  </body>
</html>
Sue Dough
Sue Dough
35,800 Points

Well it is usally over Brendan unless you use using { . Like I said there is multiple ways to write PHP tags. You are more than welcome to put all your HTML in your php. A reason one may do it the way above is because it maybe easier to read/write.

Daniel Aranzamendi
Daniel Aranzamendi
956 Points

Was it not easier to just include al the code for the foreach part inside the same php tags? Why close php tag before the <li> only to open another php tag, just to accommodate the closing "}"? I ask myself if this is valid instead

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

Instead of this:

<?php foreach($social_icons as $icon){

    ?>
    <li><a href=""><span class="icon <?php echo $icon ?>"></span></a></li>

    <?php
      }
    ?>

Any help? I get that is valid this way as presented, but just trying to understand if this is necessary, or if I can write it as the first example that I provide. Thank you.

Kevin Korte
Kevin Korte
28,148 Points

You're first example is not valid, as you have html inside the PHP. Only PHP can be inside a set of PHP tags. If you wanted to use that idea, you could, but it would have to be written like so

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

On more complicated looks, you can see how messy it can be to echo html inside of php, and keep you single and double quotes straight.

we dont we just use like this??

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

?>

at here we can say that 'icon $icon' same with 'icon twitter' |'icon facebook'|'icon google'

Kevin Korte
Kevin Korte
28,148 Points

Yeah, I missed the other icon class, but it seems you got it. Great!