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

Eoghan Uttley
Eoghan Uttley
1,076 Points

no idea, in my mind this should loop through and do them all?

Q: print each of array values to screnn

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


?>
<?php
foreach ($names as $name) {

?>

<a><?php echo '$name' ?></a>
<?php
} ?>

1 Answer

Ron McCranie
Ron McCranie
7,837 Points

So first thing, you don't necessarily need to create all of the PHP blocks you have. The task can be accomplished in one PHP block. After consolidating the broken PHP blocks, you can also remove the <a> tag and it will still print the name to the screen.

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

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

Another way of doing, more like the approach you first tried would be this:

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

<?php foreach($names as $name): ?>
  <a><?php echo $name; ?></a> 
<?php endforeach; ?>