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 Build a Simple PHP Application Working With Functions Introducing User-Defined Functions

Alton Caber
Alton Caber
2,709 Points

Need Help Outputting Sum for function mimic_array_sum


function mimic_array_sum($array) { $sum = 0; foreach($array as $element) { $sum = $sum + $sum; } return $sum; }

$palindromic_primes = array(11, 757, 16361); $sum = mimic_count($palindromic_primes); echo $sum; ?>

2 Answers

Jacques Vincilione
Jacques Vincilione
17,292 Points

Your corrected code is below.

FYI, if you're setting a variable to 0, or "", etc, you can just declare the variable without populating it. It can save a couple of bytes ;).


function mimic_array_sum($array) { 
        $sum;  //you can set the array to nothing by just declaring it.

        foreach($array as $element) { 
               $sum = $sum + $element; //the second $sum needs to be $element.

       return $sum; 

$palindromic_primes = array(11, 757, 16361); 
$sum = mimic_count($palindromic_primes); 
echo $sum; 

Adam Moore
Adam Moore
21,956 Points

I think you need your first one to be $sum = $sum + $element, so that each time the foreach function is run, it makes the new $sum equal to the previous $sum plus the next $element in the $array.