Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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!