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

Ben Os
Ben Os
20,008 Points

Stuck with counting array items to get index sum

Hi, this is my code:

<?php
$letters = array();
  $letters[] = "A" ;
  $letters[] = "C";
  $letters[] = "M";
  $letters[] = "E";
echo "Today's challenge is brought to you by the ";
count ($letters + 1 as $letter) {echo $letter};
?>

This is the relevant part of the question

Change the second echo statement to use a function to display the number of elements in $letters.

  • Note: My second echo appears in the count function row.

I tried several syntaxes to solve it. At first I tried tried to loop on the array with foreach internal loop-function, but then came a notification to use the count internal function (which I didn't know before). So as you can see above, I tried to count the variables' values with count + 1 and than echo it, but it doesn't work in that way. I will be glad to know where I was wrong,

Thanks,

2 Answers

Geovanie Alvarez
Geovanie Alvarez
21,500 Points

If you are trying to make a loop

<?php
foreach($letters as $letter){
   echo $letter;
}
?>

If you are trying to get the total of element in the array

<?php
$count = count($letters);
?>
Ben Os
Ben Os
20,008 Points

Indeed, I needed to do $count = count(variable); After that I echoed the input and it worked!

My question is, why do I need to do $count = and not just count(variable)? In other words, why do I need to put the internal_function count inside a variable which is also named count, instead of just using as it is?

Thank you!

jason chan
jason chan
31,009 Points

remember your looping the array until it becomes zero.

there are 4 arrays.