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

PHP Enhancing a Simple PHP Application Adding Search: Controller & View Accounting for Empty Results

Challenge 3 of 3 not accepting my answer

I tested this on my localhost and it works perfectly but the code challenge won't accept my response.

<?php

    $recommendations = array();
    $recommendations[] = 'Avocado Chocolate';


?><html>
<body>

    <h1>Flavor Recommendations</h1>

  <?php
if (!empty($recommendations)) {
    echo '<ul>';
    foreach($recommendations as $flavor) { 
        echo '<li>';
        echo $flavor;
        echo '</li>';
    } 
            echo '<ul>';
} else {
 echo '<p>There are no flavor recommendations for you.</p>';
}
    ?>


</body>
</html>

1 Answer

Mike Rogers
Mike Rogers
5,280 Points

Ok so the task requires you to "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 think the test validator runs into problems with this line:

<?php
    $recommendations = array();
    $recommendations[] = 'Avocado Chocolate';
?>

Because you've set the $recommendations variable twice (maybe, I'm probably wrong on that). I got mine to pass by using:

<?php
$recommendations = array('Avocado Chocolate');
?>

Either syntax is correct for adding items to an array and either should work...I just figured it out. I typed my closing ul tag incorrectly. Thanks for your help, Mike!