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!

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 Build a Simple PHP Application Listing Inventory Items More Excitement With Arrays

Sam Fitz
Sam Fitz
7,036 Points

PHP: Lesson to Echo an Array

I am not sure what I'm doing wrong for this question. Here is the question:

Right now, the unordered list does not show the flavors from our array. In the next few tasks, we'll change that. This task has two steps. (1) Add the start of a foreach loop with an opening curly brace after the opening <ul> tag but before the first opening <li> tag. The foreach loop should load each element from the flavors array, one at a time, into another variable named flavor. (2) Add the closing curly brace for the foreach loop after the final closing </li> but before the closing </ul>.

Here is my PHP code response:


$flavors = array("Chocolate", "Vanilla", "Orange Sherbert");

?> <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><?php echo $flavor; ?></li> } ?> </ul>


3 Answers


You have too many opening and closing PHP tags. Get rid of the opening and closing tags surrounding the echo statement inside of the foreach loop as you are closing the PHP and not including the curly bracers at the end of the foreach block. Do this and you should be good. Try it out.


Sam Fitz
Sam Fitz
7,036 Points


Thank you for your reply! It was because I have the close the php tag after posting the { to open the foreach statement to be executed. And then put the closing } inside its own php tag at the very end. Like this:

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

Weird syntax if you ask me. :-/ Oh well, I've learned it now!

Sam Fitz
Sam Fitz
7,036 Points

Why doesn't this forum allow the use of [code][/code] tags?