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 Build a Simple PHP Application Listing Inventory Items Creating the Products Array

Darrel Lyons
Darrel Lyons
2,991 Points

Why use a new line?

I'm not arguing against it, i was just wondering why you have to use the curly bracket on a new line like he does in the video instead of keeping in all on the same line because they both work.

In the video he says you need separate lines:

<ul>
    <?php foreach($flavors as $flavor) { ?>
    <li><?php echo $flavor; ?></li>
    <?php } ?>
</ul>

What also works:

<ul>
    <?php foreach($flavors as $flavor) { ?>
    <li><?php echo $flavor; } ?></li>
</ul>

5 Answers

Tobias Schulz
Tobias Schulz
1,997 Points

In this case maybe, but what if you doing more stuff in your loop? The more lines of code you got in it you will be happy to see the end of the loop in a new line. Changes on very old code could be hard to read cause you´ll don´t know exactly what you did there :)

For collaboration its good for all developers to do the same style of coding.

For your personal privat code you are free to code like you want :)

I would agree with Tobias Schulz that you should do things like this for readability. If you're scanning a lot of lines of code it would be easy to miss that closing brace with it hiding in that full line of code.

In this case though there's a real reason to be including it on its' own line. The two code blocks you've posted do not produce the same html.

You have taken the closing </li> out of the loop and so only the last list item will receive a closing tag.

This is the resulting html from running the two code blocks above:

<ul>
    <li>cookie dough</li>
    <li>chocolate</li>
    <li>vanilla</li>
</ul>

<ul>
    <li>cookie dough        <li>chocolate       <li>vanilla</li>
</ul>

So we can see in the second list that only the last list item gets a closing tag.

You may notice it renders fine in the browser because browsers are generally pretty good at fixing up badly formatted html. I would not recommend relying on the browser though to fix your html.

To be able to see this html you have to view the page source in your browser. If you look at the html using your browsers dev tools then you will probably see the corrected html. At least you do in firebug.

Darrel Lyons
Darrel Lyons
2,991 Points

Thanks for your reply Jason, i think it is best to stick to goo practice, i don't want to work the browser like you said.

Tobias Schulz
Tobias Schulz
1,997 Points

It´s all about readability.

You can use this as well:

<ul>
      <?php foreach($flavors as $flavor) : ?>
           <li><?= $flavor; ?></li>
      <?php endforeach; ?>
</ul>
Darrel Lyons
Darrel Lyons
2,991 Points

I find the second code just as easy to read as the top one to be honest.

Darrel Lyons
Darrel Lyons
2,991 Points

That's true. I will keep keep my scripts readable so i'm not kicking myself in the future when i read it. Thanks!