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!

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


Coding the get_products_count Function

Create a new function in products.php called get_products_count. This should receive no arguments, and it should return the total number of products. This new function should call our existing get_products_all function and return the number of elements in the products array.

the description isn't clear.. HELP!

Walden Wang
Walden Wang
5,657 Points

function get_products_count(){ return count(get_products_all()); }

This function is used to calculate the total products.

2 Answers

Juliano Vargas
Juliano Vargas
Courses Plus Student 15,575 Points

create a working variable and assign to the function then return the the count variable like this : $total = get_products_all();

return count ($total);

Michael Walker
Michael Walker
12,466 Points

I found it simpler to approach it in a more linear fashion using the working variable. You could do $total = count(get_products_all());

return $total;

If you can get away without using natural functions in the return command, the code looks a little cleaner. :-)