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 trialMUZ140045 Tafadzwa Gumunyu
13,202 Pointsstage 6 challenge 1
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.
<?php
$numbers = array(1,5,8);
array_sum(14);
echo $sum; ?>
2 Answers
Chris Shaw
26,676 PointsThe current code you have isn't quite right as you're not doing the following:
- Assigning the returned value of
array_sum
to the variable$sum
- Not passing the
$numbers
array to thearray_sum
function.
The final code for this task you should have is as follows.
<?php
$numbers = array(1,5,8);
$sum = array_sum($numbers);
echo $sum;
?>
Happy coding!
MUZ140045 Tafadzwa Gumunyu
13,202 Pointsthank you so much got it correct
Chris Shaw
26,676 PointsYou're welcome.