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 Enhancing a Simple PHP Application Paginating a List: Controller Coding the get_products_count Function

Fewer LInes

Why can't I just have one line in the function, rather than having to create a new array, populate it, and then count the elements? Such as: return count(get_products_all());

3 Answers

Ah, code challenges are very specific in what they're looking for. I prefer your answer!

Hampton Paulk might be able to update this answer, or maybe explain why it's a little long winded.

Tom is right, your code is smaller for sure. The code challenges are very picky, testing you in the way it was taught. I understand the frustration for sure.

This was on a code challenge. The single line was not accepted, but the three lines were. It is a function to get the count of the number of elements in an array. This is my code from Mike's store, I think the code challenge used $flavors but it is the same thing.

function get_products_count() {
    $products = array();
    $products = get_products_all();
    return count($products);
}

versus

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

If you can replace the code with one simpler, more readable line, then make it happen!

Unless there's a good reason that these variables have to be defined regardless. What's the original function?