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

What am i missing?

So, my code isn't working. Maybe i didn't understand question. Is my code correct?

function mimic_array_sum($array){
  $count = 0; <br>
  foreach($palindromic_primes as $palindromic_prime){
    $count = $count + 1;
  }
  return $count;
}

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

5 Answers

Scott Evans
Scott Evans
4,236 Points

Hi there Ammar.

I think you just slightly misread the question. It was asking you to 'count up' as in sum all of the numbers in the array. Please find my below example of how it should be done.

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

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

Hope this helps! Dont hesitate to ask any more questions!

Hi Scott, This challenge seems tough for me here is what i get, following your example http://awesomescreenshot.com/0962k61c65

Your code does not represent the code that Scott posted. You have a few errors in your code. You have a typo in your foreach line and you also shouldn't put the working variable $elements on the left hand side of an assignment statement. You would only use it on the right hand side as part of an expression.

Scott is using $sum += $num; This is shorthand for $sum = $sum + $num in case that was causing you some confusion.

In your screenshot you're trying to add 1 to each element. All they want you to do is add up all the numbers in the array.

$sum is keeping a running total of the sum. Each time through the loop you add on the current element in the array to your current sum.

Maybe my logic for foreach is bad or passing in variables, now i made this which has no output. Now what am i doing wrong? ```php function mimic_array_sum($array){

$count = 0; foreach ($elemets as $element){ $count = $count + $element } return $count;

}

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

Scott Evans
Scott Evans
4,236 Points
function mimic_array_sum($array){

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

}

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

What was i doing wrong? I didn't understand

Scott Evans
Scott Evans
4,236 Points

in your foreach loop you were calling an array called $elemets which doesn't exist. You need to loop through the array which was passed to the function, which in this case is $array.