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!

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


Not quite understanding, this is how i understant guiding by the Code Challenge above :

function mimic_array_sum ($array){

            $sum = 0;
        foreach ($array as $element){
        $sum = $sum + $element;

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

echo mimic_array_sum($palindromic_primes);

The way im understanding is $sum equals to "0" and the $element is the number inside of the array of $palindromic_primes. So what is doing is for example $sum = 0 ($sum) + 11 ($elements); than $sum = 0 ($sum) + 757 ($elements)... And goes on.

Sorry for my ignorance but im not quite getting how come this adds all numbers

2 Answers

This will iterate 3 times (3 elements in the array) in order. You may be interpreting the loop to act independently on each element.

So, first element = 11 sum = 0 + 11 = 11; then element = 757 sum = 11+757 = 768; then element = 16361 sum = 768+16361 = 17129;

Make sense?

PS: The tic marks you want to use are the ones on the same key as the ~ symbol next to the 1 on the keyboard. This should properly format your code.

Lol, right, it makes all sense, cant understand why i didnt notice this, thanks a lot Ryan.