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
Roland Santus
2,514 Pointsintroducing arrays challenge task 5
Hi Can't understand why this is not working the preview shows the right answer but when checking work it says: "Something is not quite right, please check code". Any help would be grateful code that I have in putted is below:
<?php
$letters = array ("D","G","L"); echo "My favorite "; echo count($letters); echo " letters are these: "; foreach($letters as $letter) { echo "AB"; echo "" ; } ?>
5 Answers
Ryan Gordon
1,340 PointsIn your foreach statement you have this
foreach($letters as $letter) {
echo "AB"; echo "" ;
}
Now, here you will see the result as ABABAB. However, your input, $letters, is DGL. So you want the output to read
My favorite 3 letters are these DGL.
Take a look at the code I picked out above? What's wrong with it? I know the answer, but that won't help you learn it.
How can you make that code display your array, letters, instead of ABABAB
Roland Santus
2,514 PointsHi Ryan
I'm guessing that if I change the echo "AB"; to echo "DGL"; that should change it?
Ryan Gordon
1,340 PointsTry thinking of variables instead of constants. If you call the constant, "DGL", it will display DGLDGLDGL.
Here, the variable is $letter. Each time the loop iterates, it will call a new element from the array $letters. So the first time through it will echo the next element in letters.
try
echo $letter
This will call letter on each iteration.
There's been a couple other loop questions today. Must be the weather.
Roland Santus
2,514 PointsHi Ryan Yes that does work but have just checked the question again and it should read ABABAB, just don't understand if the code is right why the challenge is not accepting
Ryan Gordon
1,340 PointsWhat is the exact question?
Roland Santus
2,514 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. (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've moved the closing curly brace between the 4th and 5th echo command as that was wrong but it is still not accepting,
Roland Santus
2,514 PointsSorted it out, the last echo needed to be echo "." instead of without. Thanks for the help Ryan I have noted the code you gave me for the future