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

Brecht Philips
Brecht Philips
8,863 Points

Won't accept my php code while it works..

i think my code is correct but it keeps saying using te wrong array

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

foreach ( $names as $name ) {
                    echo $name." ";
                }

?>
Brecht Philips
Brecht Philips
8,863 Points

Got it to work i changed my $name variable to $something and it accepted it. i think is didn't got the difference between $names and $name

Jason Anders
Jason Anders
Treehouse Moderator 145,858 Points

The variable name would not have mattered. In fact, PHP convention is to name the variable in a foreach loop as the singular of the array name, so you were correct in the naming convention. The error was with the added coded, not the name of the variable.

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

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

:dizzy:

2 Answers

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,858 Points

Hey Brecht,

Your code is correct, but you added some code that was not asked for by the challenge. In the echo statement, the challenge just wants you to "echo out each name." It didn't ask for any concatenation, so you just need to delete the ." " that you added.

Challenges are very picky and specific.

Keep Coding! :dizzy:

Gianmarco Mazzoran
Gianmarco Mazzoran
22,076 Points

Hi,

your code is ok except one thing, you don't need this code: ." ";

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

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

$name can be replaced with anything you want.