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
Imran Jafri
1,524 PointsStuck with this
<?php
$letters = array("D", "G", "L");
echo "My favorite ";
echo count($letters);
foreach ($letters as $letter) {
echo " letters are these ";
echo $letter;
}
echo ".";
?>
The fourth echo statement, the one inside the foreach loop, should only display one letter at a time. Change that fourth echo statement so that it does not display the static piece of text AB; instead, make it display the value for the array element under consideration inside the foreach loop.
can't figure really If can't read the question correctly or there is mistake in my code
3 Answers
Randy Hoyt
Treehouse Guest TeacherHere are some hints:
- Code inside a foreach executes once for each element in the array. This array has three elements, so anything inside the foreach loop will execute three times.
- You want the text "My favorite # letters are these:" displayed only one time.
- You want the period at the end of the sentence "." displayed only one time.
- You want the code
echo $letter;to execute three times total, once for each element in the array.
If you refresh the output for the block of code you shared above, it looks like this:
My favorite 3 letters are these D letters are these G letters are these L.
The text I have marked in bold should not be included. Your output should look like this:
My favorite 3 letters are these: DGL.
(Once you get all the code in the right place, you should also notice that the colon [:] is missing in your output. You must have removed that.)
Does all that help?
Imran Jafri
1,524 Pointsthanks Randy, the only mistake I had made was counting the echo statements ;) I should have spent more time in my math class ;)
Instead of putting foreach after 3rd echo i put it after 2nd echo :/
Randy Hoyt
Treehouse Guest TeacherHa!
In another video, I explain how to write a function that does counting ... then you can just let code do that for you. :~)
$count = $count + 1;