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

Nathaniel Lough
Nathaniel Lough
9,215 Points

Parser needs fixed for the Challenge Task 1 of 2 in PHP Basics.

The question asks me to iterate through each name in the $names array using a foreach. It won't recognize perfectly valid syntax that works even if you view it in the preview pane.

foreach( $names as $name ) will not pass the validator

foreach ($names as $name) will pass the validator.

Notice the space after '(' before '$names'. It makes all the difference (to the parser).

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

?>

1 Answer

Hey Nathaniel,

Get rid of the br tag and it should pass.

Even though it's valid syntax, the question has strict parameters for what qualifies as an answer... and adding a br tag falls outside those parameters.

Nathaniel Lough
Nathaniel Lough
9,215 Points

You can completely leave out the body of the foreach ( $names as $name ) {} and it will still fail. It has nothing to do with the body of the foreach. The code that you see in the screenshot above is the code that ended up passing. Since that code passed, we can conclude that the <br> isn't outside of any parameters.

What failed is when I used "foreach ( $names as $name ) {}". Notice the space before the $names.

I'd like to see this get fixed. In the mean time I posted this here so that other people can more quickly figure out why their valid syntax is failing so they can move on to the next challenge.

Gotcha. Did you edit your question? I don't think it mentioned the space when I read it before... maybe I didn't read it close enough. I assumed the code you posted was what wasn't passing.