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 trialolamilekan oshodi
959 Pointscode 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.
<?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
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
14,310 PointsI took your code and made these three modifications to get it to pass:
- Change it to foreach ($names as $name)
- Change $names to $name in your echo statement as well.
- 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>
gittyherz
11,818 Pointsyour 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
959 Pointsoh 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.
gittyherz
11,818 Pointshe wasn't echoing something he changed the class of the object using an array. When echoing you do need a ;
gittyherz
11,818 Pointssorry wrong part of the video.
olamilekan oshodi
959 PointsThanks guys... i understand now.
Dustin McCaffree
14,310 PointsDustin McCaffree
14,310 PointsAlmost! After that, he'll just need to change $names to $name in the echo statement, as well as in the foreach. :)