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

Juliette Tworsey
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Juliette Tworsey
Front End Web Development Techdegree Graduate 32,425 Points

Challenge Task 3 of 3-Stage 6-Working with Functions

This is extremely complex for a total beginner and there are no hints to guide me along. Once again, I have no idea on where to start.....

palindromic_primes.php
<?php 


function mimic_array_sum($array){
    $total = 0;
    foreach($array as $number) {
        $total = $total + $number;
    }
    return $total;
}

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

?>

3 Answers

Ryan Field
PLUS
Ryan Field
Courses Plus Student 21,242 Points

So here, you just need to call the function you have just created and store it in a variable called $sum and echo that to the page, like so:

<?php 

function mimic_array_sum($array){
    $total = 0;
    foreach($array as $number) {
        $total = $total + $number;
    }
    return $total;
}

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

// This line is basically saying, 'we are setting up a variable called $sum,
// and for its value, we are going to run the function mimic_array_sum
// with the argument $palindromic_primes and set it as whatever that
// results in'.
$sum = mimic_array_sum($palindromic_primes);

//Then, just echo to the page!
echo $sum;

?>
Juliette Tworsey
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Juliette Tworsey
Front End Web Development Techdegree Graduate 32,425 Points

I see. That works. Thanks!

This is what I attempted in place of $sum = mimic_array_sum($palindromic_primes):

$echo mimic_array_sum($palindromic_primes);

OY. I was way off the mark:-)

Ryan Field
Ryan Field
Courses Plus Student 21,242 Points

Actually, your code is perfectly fine and will do the same thing as mine. The only difference here is that the challenge explicitly says to store the evaluation of the function in a variable named $sum and then echo that to the page.

So you were closer than you think! :)