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

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