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 Build a Basic PHP Website (2018) Listing and Sorting Inventory Items Introducing Functions

It's been a while... I have forgotten how to structure this.

help.

index.php
<?php

$numbers = array_sum(1,5,8);

$sum = 0;
$array_sum(($numbers as $number)) {
    $sum = $sum + $number;
}

echo $sum;

?>

1 Answer

Felipe Giovanni de Moura Santos
Felipe Giovanni de Moura Santos
21,331 Points

What's up man...

The array_sum() function receives an array "as its one and only argument" and returns the sum. So when calling it, you don't have to make it's logic for it, just pass an array as the parameter and do something with the returning value (in this case you could assign it to a variable or echo it out directly).

<?php

$numbers = array(1,5,8);

$sum = array_sum($numbers);

echo $sum;

?>

or, if the only reason for making the sum is to echoing it:

<?php

$numbers = array(1,5,8);

echo array_sum($numbers);

?>