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

Introduction User-Defined Function 3of 3

It looks like you have not created a variable named $sum in your main code, outside of the function. Please create it and store the return value from the mimic_array_sum function call in it.<?php

function mimic_array_sum($sum) { $sum = 0;

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

}

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

?>

11 Answers

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

Well, I do have all the answers here in my top-secret briefcase. :-)

      ____
 .---[[__]]----.
;-------------.|
|             ||
|             ||
|             ||
|_____________|/

Bummer!It looks like you have not created a variable named $sum in your main code, outside of the function. Please create it and store the return value from the mimic_array_sum function call in it. where do you place the variable?

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

Your function code looks right. Now you need to create a variable outside of the function, like this:

function mimic_array_sum($sum) {
                            $sum = 0;

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

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

$sum = ____;

$sum = 17118; $sum = 0; $sum = return; Which one or do I need to go back to math class

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

The instructions ask you to do this:

Please create it and store the return value from the mimic_array_sum function call in it.

That means do this:

$sum = mimic_array_sum($palindromic_primes);

In earlier step, we wrote the mimic_array_sum function that receives an array of numbers and adds up all its elements. In this step, we need to call that function. We pass in an array of numbers ($palindromic_primes), and the function returns the sum of them. We then load that return value into a variable named $sum.

Does that help?

<?php

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

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

$sum = mimic_array_sum($palindromic_primes); ?> "Still say's bummer!

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

"Bummer" means you have invalid PHP code. You changed a line from what you sent me earlier. You now have this ...

<?php 

function mimic_array_sum($array) {
                            $sum $sum;         

... but you used to have this:

<?php 

function mimic_array_sum($array) {
                            $sum = 0;

It was correct before, and you'll need to change it back.

It looks like you are not displaying anything to the screen. Be sure to use an echo command.

function mimic_array_sum($array) { $sum = 0; I tried: echo sum = 0 echo 0;

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

Oh, but not inside the function. You need to do that at the end of your code.

$sum = mimic_array_sum($palindromic_primes);
echo $sum;

perfect your a genius!!