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
Tony McCabe
8,445 PointsIntroduction 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
Treehouse Guest TeacherWell, I do have all the answers here in my top-secret briefcase. :-)
____
.---[[__]]----.
;-------------.|
| ||
| ||
| ||
|_____________|/

Tony McCabe
8,445 PointsBummer!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
Treehouse Guest TeacherYour 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 = ____;

Tony McCabe
8,445 Points$sum = 17118; $sum = 0; $sum = return; Which one or do I need to go back to math class

Randy Hoyt
Treehouse Guest TeacherThe 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?

Tony McCabe
8,445 Points<?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
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.

Tony McCabe
8,445 PointsIt 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
Treehouse Guest TeacherTry this:
echo $sum;

Randy Hoyt
Treehouse Guest TeacherOh, but not inside the function. You need to do that at the end of your code.
$sum = mimic_array_sum($palindromic_primes);
echo $sum;

Tony McCabe
8,445 Pointsperfect your a genius!!