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

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,547 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 ;)