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

Stuck on Introducing Functions Code Challenge

http://teamtreehouse.com/library/introducing-functions-3

This is all I could come up with and it's not working :(

<?php
function array_sum($array){
$numbers = (1,5,8);
$sum = 0;
foreach($array as $numbers => $number) {
$sum = $sum + $number;
}
echo $sum;

?>

4 Answers

The function is created, all you need to do is call it.

It also asks you to remove the foreach loop.

So you can remove the line that starts with function.

The $numbers array holds our values.

And we call the array_sum() function and pass it the argument of our variable that stores our numbers, in this case that is the array with the values (1, 5, 8). The function will loop through each of the numbers, add them, and return the sum.

You can give the variable $sum the value of the array_sum($numbers).

Than, you just echo out the variable $sum.

So it looks something like this:

<?php
$numbers = array(1, 5, 8);
$sum = 0;
$sum = array_sum($numbers);
echo $sum;
?>

lol, I actually figured it out 5 mins after posting then the pc died. Thank you for the response though

Thanks for the response Kevin, I was a little confused on this already, but you had a nice explanation.

Thanks Kevin! Was stuck on this one for a while. Some of the quizzes/challenges are worded weird.