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

Listing Inventory Items: code challenge

I cain't see what I'm doing wrong here...

$letters = array("D", "G", "L", "R");
echo "My favorite ";
echo "3";
echo " letters are these: ";
foreach($letters as $letter){
echo $letter;
}
echo ".";

?>

3 Answers

Andrew Showalter
Andrew Showalter
14,028 Points

Lets start with the simple stuff. This challenge is looking to dynamically "count" the letters. So, your second echo statement should look like this:

<?php
echo count($letters);
?>

Overall, the statement looks pretty good, though, you MAY be missing an opening php tag according to your code you pasted. The full statement can look like this:

<?php

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

?>

Hope that helps!

Hi Tristan,

Your only problem is that you have hard-coded the number of letters: echo "3";

One of the tasks wanted you to take that out and instead replace it with a count of the array items. echo count($letters);

This way it would be dynamic and you could add however many letters you wanted.

Thanks for the help guys!