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

Lost with no hints

Right now, the text shows the wrong letters; we want it to display my real favorite letters from the array. In the next few tasks, we’ll change that. This task has two steps.

  1. Add the start of a foreach loop with an opening curly brace between the third and fourth echo commands. The foreach loop should load each element from the letters array, one at a time, into a working variable named letter.
  2. Add the closing curly brace for the foreach loop between the fourth and fifth echo commands. (Leave the letters AB in the fourth echo command for now. At the end of this step, the page should say my favorite letters are ABABAB.)

I have watched the video several times and still can't get this one right, what is the code supposed to look like? I have no idea what to try next.

8 Answers

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

Hey @Jason,

Where the code has this ...

echo "AB";

... you need to change it to this ...

foreach ($letters as $letter) {
    echo "AB";
}

Does that help?

Hi Randy, please show the rest of the code i seem to be stuck here

<?php

$letters = array("D", "G", "L");

echo "My favorite ";
echo count($letters);
echo " letters are these: ";

foreach($letters as $letter) {
  echo "AB";
}
echo ".";

?>
<?php

$letters = array("D","G","L");


echo "My favorite ";
echo count($letters);
echo " letters are these: ";
foreach ($letters as $letter) {
echo "AB";
}
  echo ".";


?>

Here's the answer for it

I'm working on this challenge ATM. I'm pretty sure I have everything right, but it still says I'm wrong. This is my code...

<?php $letters = array("D","G","L");

echo "My favorite "; echo count($letters); echo " letters are these: "; foreach ($letters as letter) { echo "AB"; } echo ".";

?>

Possibly BUG????

Dan Gorgone
STAFF
Dan Gorgone
Treehouse Guest Teacher

Hi Jason, can you be more specific about the project, stage, and code challenge you're working on? Adding tags to the post might help get the appropriate people to find your questions as well.

Hi Dan please check out my query at the bottom. stuck.

This is for Randy -- it's he BUILD A PHP APP (when he teaches about the 'foreach loop')

I forgot the context of the quiz question, so I can't be of help. sorry!

The code challenge says i have too many colons and wont pass dont seem to see where im going wrong here. Please help.

<?php

$letters = array("D", "G", "L"); echo "My favorite "; echo "Number of letters is: " . count($letters); echo " letters are these: "; foreach($letters as $letter) { echo "AB"; } echo ".";

?>

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

Take a look at the output in the right. At this step, I believe it should say this:

My favorite 3 letters are these: AB.

If you look at the output, I suspect your output has more than that. Modify the code so that it displays exactly what you see above.

Does that help?

Thanks Randy the code below worked thanks to your suggestion <?php

$letters = array("D", "G", "L");

echo "My favorite "; echo count($letters); echo " letters are these: "; foreach($letters as $letter) { echo "AB"; } echo "."; ?>