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
Evans B. Ofori
Courses Plus Student 13,720 PointsCode challenge php: Accounting for Empty Results
The following code contains a foreach loop that displays a list of ice cream flavor recommendations. It works great when the $flavors array has at least one element, but we end up with some extra HTML when we don't have any recommendations. Write a conditional that makes sure the unordered list tags and the foreach loop get executed only if the array has at least one element.
I don't know where am doing wrong: Bummer! The unordered list tags appear even when the array is empty. Place those tags inside a conditional that uses the empty function.
?php
$recommendations = array();
?><html>
<body>
<h1>Flavor Recommendations</h1>
<ul>
<?php foreach($recommendations as $flavor) { ?>
<?php if (!empty($flavor)) {
echo '<li>' . $flavor . '</li>' . "\n";
} ?>
<?php } ?>
</ul>
</body>
</html>
8 Answers
Kevin Korte
28,149 PointsYou need to write an if statement to check if $recommendations == "".
The entire foreach loop and <ul> tags should only be ran once we know the $recommendations array is not equal to empty.
So either check if it is empty, or it's not empty, you decide; and put your for each loop in the conditional so it only runs when we know there is something in it.
Does that help?
Randy Hoyt
Treehouse Guest TeacherThe $recommendations variable is an array, so you don't want to check if it is equal to an empty string. You want to check if it's empty using the empty function:
if (empty($recommendations)) {
In this code challenge, you want to check if it's not empty. If it's not empty, then you want to display the unordered list and the list items.
Does that help?
Szu Liat Ting
3,118 PointsSeemed simple but no. Below is the code with the !empty($recommendations) condition, but the response is Bummer! Try again! What's missing? Thanks!
<?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 } ?>
</body> </html>
Randy Hoyt
Treehouse Guest Teacher"Bummer! Try again!" means you have invalid PHP code. I think the issue is the spaces you have between the <? and the php in your two new opening PHP tags. Instead of this ...
<? php
... you should have this ...
<?php
Is that the issue? Your code looks correct otherwise; you definitely have the right idea, wrapping the <ul> in a conditional.
Szu Liat Ting
3,118 PointsYes, that's the issue indeed. Thanks Randy!
Evans B. Ofori
Courses Plus Student 13,720 PointsMany thanks to Kevin, Randy and Szu!
What is the real benefit of using open and closing of the conditions like this <?php if(condition) { ?> ....... <?php } ?> Instead of <?php if(condition) { ....... } ?> with proper indentation.
Randy Hoyt
Treehouse Guest TeacherThe only difference about how easy the code is to maintain. If you are mingling HTML and PHP code together, then it's probably easier to understand if you write it the first way. If you are just echoing out text, then the second way is probably easier. (We'll actually talk a little bit more about that in the next two sets of videos on pagination, which should be coming out soon.)
Evans B. Ofori
Courses Plus Student 13,720 PointsHello Randy, Thanks a lot for your quick response. This is my first contact with php and I‘m learning a lot from your videos. I like the way you teach step by step with the project, not just doing the code challenge but applying to the real world project, for me is the best. I will be looking forward to next videos.
MUZ140787 Ricardo Chitagu
5,364 PointsGuys i used szu's code without the space but still m not passing ...its jus 'bummer try again'.Im stuck