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

introducing functions

what exactly are they asking for me to do ? I already know how the array_sum() function works but in what manner do they want it . The code below creates an array of numbers, loops through them one at a time, and sums their values. PHP actually has a native function that does this same thing: array_sum(). [The array_sum() function receives an array as its one and only argument, and it sends back the sum as the return value.] Modify the code below: remove the foreach loop and the working sum variable, replacing them with a call to the array_sum() function instead.

4 Answers

Chris Willoughby
Chris Willoughby
6,792 Points

I figured out the challange. They want you to replace the for each loop with somthing smaller.

$sum = array_sum($numbers);

echo $sum;

good luck

Michael O'Malley
Michael O'Malley
4,293 Points
            $sum = 0;
            foreach($numbers as $number) {
                $sum = $sum + $number;
            }
            echo $sum;

Does the same thing as the array_sum() function.

Remove the code above and use the array_sum() function instead (echo the array_sum() instead of $sum).

Marvin Abella
Marvin Abella
6,378 Points

I believe they want you to sum up the array using a foreach. It will be helpful if you specifically put the challenge question here or just the section.

It finally clicked for me when I looked at the first example in the actual PHP manual here.

http://php.net/manual/en/function.array-sum.php