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
Lyric Abbott
35,595 Pointshelp challenge 3
Let's make sure that foreach loop executes correctly if the $recommendations array is NOT empty. Add one element to the $recommendations array with a value of 'Avocado Chocolate'.
my code:
<?php
$recommendations = array();
$recommendations[] = 'Avocado Chocolate';
?><html>
<body>
<h1>Flavor Recommendations</h1>
<?php if(!empty($flavors)) { ?>
<ul>
<?php foreach($recommendations as $flavor) { ?>
<li><?php echo $flavor; ?></li>
<?php } ?>
</ul>
<?php } else { ?>
<p><?php echo 'There are no flavor recommendations for you.'; ?></p>
<?php } ?>
</body>
</html>
2 Answers
Vittorio Somaschini
33,371 PointsHello Lyric.
I had a quick look at your code before testing it.
Shouldn't the IF statement refer to $recommendations rather than $flavors? when checking if empty or not? I would simply change that.
Let me know if it works. ;)
Vittorio
geoffrey
28,736 PointsYes vittorio is right, that's the mistake. I don't know why you've changed this value, you have to leave it as you typed it previously and just add the asked value to the array as you did once again at the top of your code.
<?php
$recommendations = array();
$recommendations[]='Avocado Chocolate';
?>
<html>
<body>
<h1>Flavor Recommendations</h1>
<?php if(!empty($recommendations)) { ?>
<ul>
<?php foreach($recommendations as $flavor) { ?>
<li><?php echo $flavor; ?></li>
<?php } ?>
</ul>
<?php } else { ?>
<p><?php echo 'There are no flavor recommendations for you.'; ?></p>
<?php } ?>
</body>
</html>