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

PHP Basics > Stage 5 > PHP Conditionals & Loops > Challenge task 1 of 2...

Not sure what I've got to do? I've got

...

foreach( $names ) { // run code, but what? echo the names? }

— thanks for your help, I really enjoy this course I think it's well explained. Although, I think I'll go over it all a coupla times just to make sure it has sunken in!

6 Answers

Sjors Theuns
Sjors Theuns
6,091 Points

Hi Philip,

the challenge states: "Using a foreach, create a loop that goes through each of the names in the $names array.".

So foreach is the way to go. Create something like:

foreach($names as $a_name)
{
echo $a_name . "<br />";
}

It doesn't really matter what you do inside the loop. In this example I just echoed the names with an html break.

The variable $a_name is just a random chosen name, you can make it as you want.

ha! The minute I posted the question, I figured it out.

Thanks for your Sjors, this is what I got...

<?php

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

foreach($names as $name) { echo $name; } ?>

— I think sometimes I take the question far too literal,

Thanks again!

Sjors Theuns
Sjors Theuns
6,091 Points

Glad you figured it out as well. The important thing in this challenge is just to loop all names from the array, the challenge also accepts an empty foreach like:

foreach($names as $a_name) {
}

:-)

Ps. don't forget to close your question, otherwise it will keep open

Emanuel Diaz
Emanuel Diaz
7,291 Points

I sent an email to support team. I tried javascript syntax, echoing the variable as a string, and various other things to try to get it to work, but no luck. I'll post on here if it get's fixed.

Punal Chotrani
Punal Chotrani
8,817 Points

This was my code

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

foreach($names as $first_name) { echo $first_name . "<br />"; } ?> '''

I'm pretty sure this is correct. There is definitely something wrong with the php parser.

<?php $names = array('Mike', 'Chris', 'Jane', 'Bob'); foreach ($array as $names){ echo $names; } ?>

how come it isnt working???

Sjors Theuns
Sjors Theuns
6,091 Points

You are mixing up the variable names. It should be something like:

<?php
$names = array('Mike', 'Chris', 'Jane', 'Bob');
foreach ($names as $a_name){ echo $a_name; }
?>

Your first line is fine, the problem was the foreach

Emanuel Diaz
Emanuel Diaz
7,291 Points

My code still would not work I cant seem to find what is wrong with it? Keeps telling me "Bummer! I do not see a foreach statement"

<?php $names = array('Mike', 'Chris', 'Jane', 'Bob'); foreach($names as $a_name) { echo $a_name; } ?>

Sjors Theuns
Sjors Theuns
6,091 Points

Hi Emanuel,

I doubt there is something wrong with your code. I tested the code outside the online editor and it works fine. Strange fact is that the code also worked a few months ago on the treehouse editor.

Perhaps there is a (new) bug in the treehouse PHP parser? Try contacting the support directly via https://teamtreehouse.com/support

If you find out what's wrong, please let us know in this thread. Thanks.

Grtz Sjors