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

Fernando Gomez
Fernando Gomez
18,527 Points

what's wrong with my code?

function mimic_array_sum($array) {

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

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

?>

4 Answers

This is everything in the code editor when I just passed it again.

Question 2

    <?php 

    function mimic_array_sum($array) {

    $sum = 0;
      foreach($array as $element) {
        $sum = $sum + $element; // Not 1 
    }
      return $sum;
    }



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

    ?>

Question 3

    <?php 

    function mimic_array_sum($array) {

    $sum = 0;
      foreach($array as $element) {
        $sum = $sum + $element; // Not 1 
    }
      return $sum;
    }



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

    ?>

Took me a while but it should be this think you must have had a momentary lapse in concentration

     function mimic_array_sum($array) {

    $sum = 0;
      foreach($array as $element) {
        $sum = $sum + $element; // Not 1 
    }
      return $sum;
    }
Fernando Gomez
Fernando Gomez
18,527 Points

still not working.....I changed it to $sum = $sum + $element

Fernando Gomez
Fernando Gomez
18,527 Points

Thank you Adam! Now it worked. I think I was getting ahead of myself, and I was doing question 3 in question 2, and I think that's the reason it wasn't going through.

I concur :)

Now its solved click best answer and others can see then.

Good Luck!