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

Devin Scheu
Devin Scheu
66,191 Points

Help with array's in PHP

Here's 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's My Code:

<?php


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


?>

<p>Welcome to Ye Olde Ice Cream Shoppe. We sell <?php echo 3; ?> flavors of ice cream.</p>

<ul>

  foreach ($flavors and flavor) {

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

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

  }

</ul>

1 Answer

Jason Cullins
PLUS
Jason Cullins
Courses Plus Student 4,893 Points

Ok, lets examine some of your code above to see how we can tidy this up a bit, shall we? :)

So you're declaring your array:

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

then your stopping the php output with ?> and then using regular html / text

So you have:

Welcome to Ye Olde Ice Cream Shoppe. We sell <?php echo 3; ?> flavors of ice cream.

Which will say that you sell 3 flavors of ice cream, but we can be a bit more dynamic there in case you or someone else adds say "Mint" to the array. You could do this:

Welcome to Ye Olde ice Cream Shoppe. We sell <?php echo(count($flavors)); ?> of ice cream.

In the line above you're echo'ing the count of items that's inside of the array $flavors. So if you have 5 items, it will output 5 and so on.

Then you setup a foreach loop because you want to show each flavor in a html list, but the syntax above is wrong. You have to declare a php block again by using <?php then do the foreach. You may have just omitted it above, but here is what the code should look like

<?php

foreach ($flavors as $flavor) {
 echo("<li>" . $flavor . "</li>");
}

?>

What the above code says in plain english is "[ForEach] Item in the array [$flavors], i want you to declare the next item [as] [$flavor] and then do what ever is inside of my block", which in your case is echo the name of the flavor. So the foreach will loop thru the array by however many items are in the array and each time it will reset the $flavor variable to whatever point in the array the foreach is in.

I hope this makes sense. If you have any other questions, just let me know!