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 Basics (Retired) PHP Conditionals & Loops PHP Loops Challenge

olamilekan oshodi
olamilekan oshodi
959 Points

code challenge - stuck foreach loops

So am doing great all of the sudden Bummer. The second question wanted to echo each foreach loop to the screen and i thought have done already.

The error stated he can't find semi-colon.

index.php
<?php 
$names = array('Mike', 'Chris', 'Jane', 'Bob');

?>

<pre>
  <ul>
    <?php
      foreach ( $names as $names ){
    ?>
      <li><a href=''><span class='<?php echo $names ?>'></span></a></li>
    <?php   
      }
    ?>
  </ul>
</pre>

7 Answers

Paul Ryan
Paul Ryan
4,584 Points
<?php 
$names = array('Mike', 'Chris', 'Jane', 'Bob');

?>

<pre>
  <ul>
    <?php
      foreach ( $names as $name ){
    ?>
      <li><a href=''><span class='<?php echo $name; ?>'></span></a></li>
    <?php   
      }
    ?>
  </ul>
</pre>

So close! You forgot the semi colon after names (which should be name)

Also ensure you are looping over every name in names

Dustin McCaffree
Dustin McCaffree
14,310 Points

Almost! After that, he'll just need to change $names to $name in the echo statement, as well as in the foreach. :)

Dustin McCaffree
Dustin McCaffree
14,310 Points

I took your code and made these three modifications to get it to pass:

  1. Change it to foreach ($names as $name)
  2. Change $names to $name in your echo statement as well.
  3. Add a semicolon after 'echo $name;'

Final code:

<?php 
$names = array('Mike', 'Chris', 'Jane', 'Bob');

?>

<pre>
  <ul>
    <?php
      foreach ( $names as $name ){
    ?>
      <li><a href=''><span class='<?php echo $name; ?>'></span></a></li>
    <?php   
      }
    ?>
  </ul>
</pre>

your almost there it should be like this, $names as $name and remember to end your echo with a semicolon. I don't know how to make the code appear inline.

<?php $names = array('Mike', 'Chris', 'Jane', 'Bob');

?>

<pre> <ul> <?php foreach ( $names as $name ){ ?> <li><a href=''><span class='<?php echo $name; ?>'></span></a></li> <?php
} ?> </ul> </pre>

olamilekan oshodi
olamilekan oshodi
959 Points

oh bugger... but how did that come about. what is the purpose of the semi-colon after the name, although i suppose is to differentiate the $names. but he didn't use the semi-colon himself in the lesson. see below

<ul class="social"> <?php /// Foreach loop exercise foreach($social_icons as $icon){ ?> <li><a href=""><span class="icon <?php echo $icon ?>"></span></a></li> <?php } ?> </ul>

can explain different does it makes.

he wasn't echoing something he changed the class of the object using an array. When echoing you do need a ;

sorry wrong part of the video.

olamilekan oshodi
olamilekan oshodi
959 Points

Thanks guys... i understand now.