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

More Excitement With Arrays

HELP! what is wrong with this block of code??

<?php

$flavors  = array("Chocolate","Vanilla","Strawberry");

?> <p>Welcome to Ye Olde Ice Cream Shoppe. We sell <?php echo count($flavors); ?> flavors of ice cream.</p> <ul><?php foreach ($flavors as $flavor) { <li> echo $flavor; </li> }?>

</ul>

5 Answers

I'm not sure which lesson you are refering to, but the only problem I see is that the output would show as:

Welcome to Ye Olde Ice Cream Shoppe. We sell 3 flavors of ice cream. ChocolateVanillaStrawberry

While you may be trying to get:

Welcome to Ye Olde Ice Cream Shoppe. We sell 3 flavors of ice cream. Chocolate Vanilla Strawberry

Note the only difference is the spaces between the output of flavors. It's hard to tell because you don't specify what lesson you are currently on.

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

It's supposed to look like this:

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

It can be confusing how to mingle PHP and HTML, and I'm working on something to improve how I cover that in the part of the video. I've discussed this thoroughly in another thread. Can you take a look at this comment and let me know if that clarifies things?

@ Randy Hoyt

Appreciate the replies. The lessons are quality and very readable. I am concerned the code is largely comment free. I am commenting in my files though.

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

Hey @Simbarashe Magorimbo,

Thanks for the suggestion about comments! I am adding a video about commenting at the end of the second project. Most of the code we have written probably doesn't need comments because it is easy to recognize what it does when you come back to it. For example, This is NOT necessary:

// add the two numbers together
$sum = $num1 + $num2;

I'll explain how and when to add comments, I'll go back through the project files and add comments where I think they are necessary, and I'll encourage people to download the code and look through them.

Thanks again!

:thumbsup: