Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP Build a Simple PHP Application Working With Functions Introducing User-Defined Functions

Sajid Latif
seal-mask
.a{fill-rule:evenodd;}techdegree
Sajid Latif
Full Stack JavaScript Techdegree Student 22,368 Points

Need help. What to do guys/girls? :)

Whats the issue here?

palindromic_primes.php
<?php 

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



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

?>

2 Answers

Alex Heil
Alex Heil
53,534 Points

hey Sajid Latif , looking at your code I can see 2 issues, let's tackle them:

first the small one: in your return statement (perhaps a typo) you currently have an equal sign that shouldn't be there. usually with an equal sign you assign a value to a variable, that's not what we want to do here ;)

so the correct line would be:

return $sum; 

that's it.

now the slightly bigger problem: the code inside your foreach loop is not working with the variables you actually want to pass to it, the main reason being that you don't use the variables at all. so line by line:

  foreach($array as $element){

this line, your declaration, is totally fine. you load the array $array and turn each item into a working variable $element. great!

now the codeblock:

 $sum = $sum; 
  }

here you're telling that $sum (which is 0 at that point) to be $sum. so you're basically saying 0 = 0. but actually you want to add $element to the sum. if you re-write the line to use the variable instead it will work fine, like so:

 $sum = $sum + $element; 
  }

with these 2 issues fixed your code should work just fine. hope that explanation helped you better understand what's going on ;)