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

php challenge 2 of 3

What am I doing wrong here? I only get the answer: Try again!

<?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 } ?> <?php else { ?> <p> <?php echo 'There are no flavor recommendations for you.'; ?> </p> <?php } ?>

</body> </html>

10 Answers

You didn't close the if brace from the if - else properly. Here

<?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 } else { ?>
    <p>There are no flavor recommendations for you.</p>
  <?php } ?>
</body>
</html>

what's needs to be done? which challenge is this?

If the array is NOT empty, the foreach loop will execute. But if it IS empty, we should display a message. Add an else block to the conditional that displays the following message in an HTML paragraph tag: 'There are no flavor recommendations for you.'

please put the code here but use the code markup so i can read the code better...

Hi Arvid, I will suggest some changes for you to do...first you have extra <?php} ?> written here...

<?php } ?> <?php } ?> <?php else { ?> 

Also, it asks you to create a paragraph so you have to use HTML paragraph tags

<p></p>

and not echo. These are few suggestions. I can't really see your code well. It needs to be markdown like the other ones.

he doesn't have an extra { one is for the loop and one for the if

No he has actually written it wrong. Thanks for bring it to my attention XD because I forgot to change that part, it suppose to be written like this

  <?php } else { ?>

so...overall..

<?php } ?> <?php } else { ?> 

you are correct, but maybe he doesn't know... :P

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

Markdown starts and ends with ```?

Write ``` followed by php. Click enter, paste your code. Click enter again and type 3 backticks.

<?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 else { ?> echo "<p>There are no flavor recommendations for you.</p>";
             <?php } ?>
</body>
</html>
<?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 }else{ ?>
<p>There are no flavor recommendations for you.</p>
<?php } ?>
</body>
</html>```

use this...

OK, I got it curly braces the wrong way. Thank you Gloria!

Yep. You are welcome :)