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

PHP User Defined Functions - Challenge

The following code is resulting in an error message that no value is returned. Since there is a return command at the end of the function, it should return $total. I am stumped on this.

<?php function mimic_array_sum ($array) { $total = 0; foreach ($array as $element) { $total = $total + $array[$element]; } return $total; } $palindromic_primes = array(11, 757, 16361); ?>

2 Answers

Hi keithhome,

I'm thinking that your function is returning an undefined value.

You have $total = $total + $array[$element];

Your working variable $element already contains a number from the array. You're then trying to use that as an index into the array and add that result to $total.

It should be $total = $total + $element;

Thank you for your help. :-) Keith