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

Introducing User-Defined Functions: mimic_array_sum() challenge #3 confusion.

In the main code, outside of the function, use the new mimic_array_sum() function you just wrote. Store the return value in a variable called $sum, and then display that sum to the screen.

Now I believe I could create the variable above the function which would outside or below it which would be outside, so between that and calling the function error message, I am getting more confused. Any help, first time asking question in here since starting the PHP development.

<?php

function mimic_array_sum($array) { $total = 0; foreach($array as $number){ $total = $total + $number; } return $total; } $sum = $total; $palindromic_primes = array(11, 757, 16361); call_user_func_array('mimic_array_sum', $sum);

?>

2 Answers

Stone Preston
Stone Preston
42,016 Points

here are your issues:

$sum = $total;
 $palindromic_primes = array(11, 757, 16361);
 call_user_func_array('mimic_array_sum', $sum);

you are correct in that you can place your $sum variable outside the function by placing it below it, but you are assigning it the wrong value. $total is outside of the scope since its defined inside the function, it can only be accessed there, not outside it. you are supposed to assign $sum a value using your new mimic function and the palidromic_primes array thats defined for you and echo it like so (place it after your function definition):

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

Thank you for your help, It worked.