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 trialzweih chong cheah
5,957 Pointsaccounting-for-empty-results-2 2 of 3 challenge task
If the array is NOT empty, the foreach loop will execute. But if it IS empty, we should display a message. Add an else block to the conditional that displays the following message in an HTML paragraph tag: 'There are no flavor recommendations for you.' May i know what is wrong with my code?
<?php
$recommendations = array();
?><html>
<body>
<h1>Flavor Recommendations</h1>
<?php if(!empty($recommendations)) { ?>
<ul>
<?php foreach($recommendations as $flavor) { ?>
<li><?php echo $flavor; ?></li>
<?php } ?>
</ul>
<?php } ?>
<?php else { ?>
<p>There are no flavor recommendations for you.</p>
<?php } ?>
</body>
</html>
1 Answer
Becky Hirsch
14,069 PointsHello Zweih!
I had trouble with this one too. I put your code in the challenge and it seems that when I use less php <?php ?>
tags and echo out the HTML like in the example below then it works. Everything else looks great to me. I hope this helps!
<?php
$recommendations = array();
?><html>
<body>
<h1>Flavor Recommendations</h1>
<?php if(!empty($recommendations)) {
echo "<ul>";
foreach($recommendations as $flavor) {
echo "<li>" . $flavor . "</li>";
}
echo "</ul>";
}
else {
echo "<p>There are no flavor recommendations for you.</p>";
} ?>
</body>
</html>
Justin Black
24,793 PointsJustin Black
24,793 PointsI concur -- I've done PHP for years and have so far been doing these challenges without watching any of the videos. I tried all of the industry standard stuff of !empty, isset && !empty, isset && sizeof($) > 0.. etc..
This is one case, where teamtreehouse is NOT teaching standards...