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

Kyle Damken
Kyle Damken
10,633 Points

Why is this wrong

It says "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 tested it on a my server and it works fine.

What's wrong here?

index.php
<?php

    $recommendations = array('Avacado 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>

1 Answer

Rather than having

 <?php } else {
      echo '<p> There are no flavor recommendations for you. </p>';
    }


  ?>

try

<?php } else { ?>

   <p> There are no flavor recommendations for you. </p>

<?php } ?>

What you wrote is fine it's just it can be funny since it wants you to break up the if and else statements

Kyle Damken
Kyle Damken
10,633 Points

Awesome! It worked, thanks man. It's such a pain when it can be that finicky.

No worries mate haha welcome to the world of PHP