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 trialDaniel Silva
10,644 PointsNot sure what function to use...
The second echo statement was set up to display the number of letters, but it always showed "2", and we have additional elements in $letters now. Change the second echo statement to use a function to display the number of elements in $letters.
This is what my code looks like. <?php $letters = array("A", "C", "M", "E");
echo "Today's challenge is brought to you by the "; function display() { return $letters[]; } echo " letters: "; echo $letters; echo ".";
?>
2 Answers
jcorum
71,830 PointsPHP has a function to get the length of an array. If the array name is $letters, then count($letters) will return the length.
Ford Heacock
18,068 PointsHey Daniel,
As Jcorum stated, use the count() function to return the length. Below is a working example:
<?php
$letters = array('A', 'C', 'M', 'E');
echo 'Today's challenge is brought to you by the ';
echo count($letters);
echo 'letters: ';
echo 'AC';
echo '.';
?>
Daniel Silva
10,644 PointsDaniel Silva
10,644 PointsThank you!
Victor Musvibe
10,361 PointsVictor Musvibe
10,361 Pointsbut cant i use the sizeof method?
for example
This also works and returns a value of 4 in my IDE but doesn't work in workspaces. Any idea what might be causing this not to work in workspace?