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

Chase Snow
Chase Snow
32,932 Points

Confused

I am not really sure why this is wrong, any help would be much appreciated!

Andrew Shook
Andrew Shook
31,709 Points

Could you post some code or elaborate on your problem a little bit.

5 Answers

Andrew Shook
Andrew Shook
31,709 Points

Ok i found your problem. Hint- In the function look at your return statement.

Chase Snow
Chase Snow
32,932 Points

Whoops..

This is the question the code challenge asks;

Write the code inside this mimic_array_sum() function. That code should add up all individual numbers in the array and return the sum. You’ll need to use a foreach command to loop through the argument array, a working variable to keep the running total, and a return command to send the sum back to the main code.

this is what I have and it appears to be incorrect;

<?php 


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


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

?>

Hi Chase,

If you look to the bottom right of the text box you will see something that says Markdown Cheetsheet. If you don't understand that maybe this will make sense: How to display code at Treehouse

Jeff

Chase Snow
Chase Snow
32,932 Points

Ah I see, thank you for all the advice, I have been on the site few months but haven't used the forum yet, your help is appreciated

<?php

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

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

echo $total;

?>
Robert Walker
Robert Walker
17,146 Points

Hi Chase,

You have one thing wrong:

<?php
function mimic_array_sum($array) { 
  $sum = 0; foreach($array as $number) { 
    $sum = $sum + $number; 
  } 
  return $number; 
}
?>

In the above code you are returning the $number but the function is adding $sum + $number and placing that result into $sum.

So you should have $sum as the return.

Without checking I assume the current result would only return the last number in the array 16361.

The below code should solve your problem, it is easy to overlook.

<?php
function mimic_array_sum($array) { 
    $sum = 0; 
          foreach($array as $number) { 
          $sum = $sum + $number; 
  } 
  return $sum; 
}

?>

Hope this helps.

Christopher Hall
Christopher Hall
9,052 Points

Robert Walker's answer is correct, but it's good to note that $number here is a local variable to the foreach function. You would probably get an error if you tried to return it from the mimic_array_sum function since it doesn't exist outside of the foreach.