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 Build a Simple PHP Application Listing Inventory Items Introducing Arrays

Vidit Shah
Vidit Shah
6,037 Points

Whats wrong with this php task?

<?php $letters=["D","G","L"]; foreach($letters as $letter) { echo $letter; } echo "My favorite "; echo count($letters); echo " letters are these: "; echo "AB"; echo ".";

?>

letters.php
<?php
$letters=["D","G","L"];
foreach($letters as $letter)
{
  echo $letter;
}
echo "My favorite ";
echo count($letters);
echo " letters are these: ";
echo "AB";
echo ".";

?>

1 Answer

Sean T. Unwin
Sean T. Unwin
28,690 Points

I think the order of your statements may be not be correct for what the challenge wants as expected output. Also at some point you have to remove or comment out the echo 'AB'; line.

Here is my result for the challenge. I don't like to give answers to copy/paste, but I can see how this may a little confusing. Also, my code may not be the only way to pass, although I think order of statements is important.

<?php
$letters[] = 'D';
$letters[] = 'G';
$letters[] = 'L';
$letters[] = 'R';
echo "My favorite ";
echo count($letters);
echo " letters are these: ";
foreach ($letters as $letter) {
  echo $letter;
}
//echo "AB";
echo ".";

?>