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

Not 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

PHP has a function to get the length of an array. If the array name is $letters, then count($letters) will return the length.

Thank you!

but cant i use the sizeof method?

for example

<?php 

$letters = array("A", "C", "M", "E");

echo sizeof($letters);

?>

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?

Ford Heacock
Ford Heacock
18,068 Points

Hey 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 '.';

?>