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 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:

<?php

$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>

/* ANY HELP APPRECIATED! */

3 Answers

Hello,

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.

Cheers!

Sam Fitz
Sam Fitz
7,036 Points

Shawn,

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?