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 trialTong Vang
9,926 PointsChallenge #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'.
I've used:
"<?php array_unshift($recommendations,"Avocado Chocolate"); ?>"
$recommendations = array() . "Avocado Chocolate";
It won't take them....
5 Answers
Stone Preston
42,016 Pointsyour first one should have worked. just make sure you have it inside the first php block in the file and under the code that initialized the recommendations array
<?php
$recommendations = array();
array_unshift($recommendations,"Avocado Chocolate");
?>
Tong Vang
9,926 PointsDid that before I started the discussion and no go...
Stone Preston
42,016 Pointscan you post all the code you have in the file. I just passed using that code above
Tong Vang
9,926 Points $recommendations = array();
array_unshift($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 exit;} ?>
<p>There are no flavor recommendations for you.</p>
</body>
</html>```
Stone Preston
42,016 Pointsyou did not do task 2 correctly and thats causing task 3 not to pass. You were supposed to use an else block and display that there are no recommendations paragraph within that else block. remove that exit statement and add an else block
<?php
$recommendations = array();
array_unshift($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 {
echo "<p>There are no flavor recommendations for you.</p>";
} ?>
</body>
</html>
Tong Vang
9,926 Points $recommendations = array();
array_unshift($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 {
echo "<p>There are no flavor recommendations for you.</p>"
}?>
</body>
</html>```
Still said no.