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

General Discussion

Simple PHP Application Stage 4 More excitement with Arrays challenge 6/7 STILL NEED HELP

QUESTION Inside the foreach loop, we should only display one tag for each flavor. The one list item should display the value for the array element that the foreach loop is considering. Remove one of the elements, and change the value that gets displayed in the other to display the flavor.

MY ANSWER <?php

$flavors = array("Chocolate","Vanilla", "Strawberry")

?> Welcome to Ye Olde Ice Cream Shoppe. We sell <?php echo count($flavors); ?> flavors of ice cream. <?php foreach ($flavors as $flavor) { ?> echo $flavor <? } ?>

I have tried a million different things please help

ā€¢ General Discussion

4 Answers

Stone Preston
Stone Preston
42,016 Points

currently your php tags are a bit incorrect (there may be other stuff but cant tell without looking at the challenge itself). you have

<?php
$flavors = array("Chocolate","Vanilla", "Strawberry")
?> 
Welcome to Ye Olde Ice Cream Shoppe. We sell <?php echo count($flavors); ?> flavors of ice cream.
 <?php foreach ($flavors as $flavor) { ?>
 echo $flavor 
<? } ?>

however the echo $flavor is not inside any php tags. you need to put it in some tags. you should also probably include semicolons at the end of each statement, even if its the only line and not technically necessary

<?php
$flavors = array("Chocolate","Vanilla", "Strawberry");
?> 
Welcome to Ye Olde Ice Cream Shoppe. We sell <?php echo count($flavors); ?> flavors of ice cream.
 <?php foreach ($flavors as $flavor) { ?>
 <?php echo $flavor; ?>
<? } ?>

can you post a link to the challenge

I got it thank you

I could not find my mistake . Can Someone help me ?

<?php $flavors = array("Chocolate", "Vanilla", "Cookie Dough"); ?> <p>Welcome to Ye Olde Ice Cream Shoppe. We sell <?php echo "3"; ?> flavors of ice cream.</p> <ul> <li><?php echo $flavors[1]; ?></li> <li><?php echo $flavors[2]; ?></li> <li><?php echo $flavors[3]; ?></li> </ul>

Hi durul,

The foreach loop takes the array $flavors and goes through it one element at a time putting the value into $favor (the working variable). So each time the loop iterates $flavor holds the next value in the array. So, the echo statement echos another value. One of The fun parts of this is mixing the php and html. Their are different was of doing this and with some practice it will become easier. Below are two different ways of writing the code. The first one was used in The code challenge.

Jeff

<?php

$flavors = array("Chocolate", "Vanilla", "Blackcherry", "Cookie Dough");

?>
<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>
  <?php } ?>
</ul>

or +++++++++++++++++++++++++++++++++++
<?php

$flvors = array("Chocolate", "Vanilla", "Black Cherry", "Cookie Dough");

?>
<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) {
      echo "<li>" . $flavor . "</li>";
    }
    ?>
</ul>

thanks.... :)