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
Jason Blomquist
2,300 PointsArrays
Where is my coding error? The code block below displays a list of flavors available at an ice cream store. In this code challenge, we'll modify this block to use an array so that itβs easier to add a new flavor. Change the line that creates the $flavor1 variable so that it instead creates an array called flavors. Give that flavors array one element with a value of "Chocolate". Important: The code you write in each task should be added to the code written in the previous task. Recheck Work Bummer! It looks like you haven't created an array named $flavors.
flavors.php
1 <?php 2
3 $flavors = array(); 4 $flavors = "Chocolate"; 5
6
7 ?> 8 <p>Welcome to Ye Olde Ice Cream Shoppe. We sell <?php echo "2"; ?> flavors of ice cream.</p> 9 <ul> 10 <li><?php echo $flavor1; ?></li> 11 <li><?php echo $flavor2; ?></li> 12 </ul>
2 Answers
Joe Grable
6,284 Pointsput all the flavors in an array called $flavors, then you can use the php count function to get the number of items in the $flavors array. I stored the number of flavors in an array called $num_of_flavors.
To display the number of flavors, "echo" out the $num_of_flavors. Then to display each ice cream flavor, use a foreach loop.
<?php
$flavors = array("chocolate", "vanilla", "strawberry"); $num_of_flavors = count($flavors);
?>
Welcome to Ye Olde Ice Cream Shoppe. We sell <?php echo $num_of_flavors ?> flavors of ice cream.
<?php foreach ($flavors as $flavor) { echo $flavor; } ?>
Michael O'Malley
4,293 PointsThe goal of the step is to create an array with the value of Chocolate.
With your;
$flavors = array();
You create an array, but with your;
$flavors = "Chocolate";
You're not adding it to the array. You're simple changing $flavors to a variable that equals "Chocolate".
You can add a value to an array like so;
$flavors[] = "Chocolate";
Which is what I think you were original trying to accomplish. Hope this helps. :)