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 trialCortney Burgess
10,758 Points1) 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 bra
$letters= array("D","G","L"); echo "My favorite "; echo "3"; echo " letters are these: "; echo "AB"; echo ".";
Cortney Burgess
10,758 PointsRight 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
Logan R
22,989 Pointslol, yes I got that, but what is your question about it? Are you asking what the answer is? Are you confused on some part of it?
Cortney Burgess
10,758 Pointsi need the answer i don't understand what it is asking
4 Answers
Logan R
22,989 PointsAh OK, so what they want you to use is a foreach loop.
foreach($arrayString as $newArrayString) {
echo $newArrayString;
}
$arrayString is the value of the string array. In this case, it would be $letters.
$newArrayString is what the $arrayString is renamed too, in this case $letter.
The foreach loop will echo each array value one at a time and will start with the code right after the {, then stop at the } and go back to the { until all arrays have been echoed.
If you are not completely understanding this, they go over how to use a foreach loop in great detail a video or two before the coding quiz :)
Cortney Burgess
10,758 PointsThe fourth echo statement, the one inside the foreach loop, should display my favorite letters from the array INSTEAD of AB. Remove the static piece of text from that fourth echo statement, changing it instead to display the value for the array element under consideration inside the foreach loop.
$letters = array("D","G","L"); echo "My favorite "; echo "3"; echo " letters are these: "; foreach($letters as $letter){ echo $letter[D]; echo $letter[G]; echo $letter[L]; echo "AB"; } echo ".";
Logan R
22,989 PointsCloser.
foreach($letters as $letter){
echo $letter;
}
It will auto cycle through all the letters.
Cortney Burgess
10,758 Pointsit didnt work.
Louis Philippe Savard
5,378 PointsYou have to close your curly brace after echo"AB"
Logan R
22,989 PointsLogan R
22,989 PointsCan you please clarify the question?