Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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?