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

i cant solve this

<?php $flavors=array("Chocolate","Vanilla","milk");

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

2 Answers

<li><?php echo $flavor1; ?></li>
<li><?php echo $flavor2; ?></li>}

OK, firstly, you're calling a variable that doesn't exist. Which is $flavor - the variable containing the array is $flavors.

Secondly, you're actually technically calling two variables that don't exist - $flavor1 and $flavor2.

To call specific items in an array you need to add square brackets to the end of the array variable, so if we want to call the first two items / flavors in the array, we do this:

<li><?php echo $flavors[0]; ?></li>
<li><?php echo $flavors[1]; ?></li>

This should work.

First, you need to format your code quote using the Markdown Cheatsheet syntax below. It will make your code readable.

I think you are stuck on step 4. You need to substitute out the number 2 for a dynamic way that will change based on the number of items in the array. Rather than just giving you the code to quote, I will tell you that you need to use the count() function. Here is the documentation for the function. Let me know if you still have problems.

DO it the dynamic way. Much better.