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

Devin Scheu
Devin Scheu
66,191 Points

Help With Php Functions

Question: 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

My Code:

function mimic_array_sum($array){

  $count = 0;

  foreach($array as $element){

  $count = $count + 1;

    $array[$count]= $array[$count] + $array[$count];

  }


  return $count;

}


$palindromic_primes = array(11, 757, 16361);
Aaron Elliott
Aaron Elliott
11,738 Points

Also, make sure to use the back-tick symbol when posting code. On a mac keyboard it is the symbol above the tab key. Hold alt + that key to make the back-tick. It also helps to put a line between the three back-ticks, and the code.

7 Answers

Aaron Elliott
Aaron Elliott
11,738 Points

You were close, and sorry to just give you the answer. See where you went wrong though?

function mimic_array_sum($array){
  $count = 0;
  foreach($array as $element){

    $count = $count + $element;

  }
  return $count;

}




$palindromic_primes = array(11, 757, 16361);
$count = mimic_array_sum($palindromic_primes);
$sum = $count;
echo $sum;
Aaron Elliott
Aaron Elliott
11,738 Points

No problem Devin!

Again sorry to just give the answer and run man. I am working on a school project right now. :p If you have any particular questions on using the the foreach loop feel free to ask.

Did you specifically request for me to answer your question as my notification specified? If so I appreciate it man.

Here is a more detailed explanation of what is happening here,

//Create a new function call mimic_array_sum
function mimic_array_sum($array){ 

//Define the count variable and give it a default value of 0
  $count = 0;

//call a foreach loop that loops through the $array 

  foreach($array as $element){


    $count = $count + $element;   //add each value in the array to the previous $count value
      // the foreach loop does this for each value stored in the $array that is passed into the function
  }
//after it's done it returns the total 
  return $count;

}




$palindromic_primes = array(11, 757, 16361); // Array that holds our three numbers
$count = mimic_array_sum($palindromic_primes);// The $palindromic_primes  variable is then passed into the mimic_array_sum() function
$sum = $count; // Set the $sum variable to the return value from the  mimic_array_sum() function
echo $sum; // echo the result
Devin Scheu
Devin Scheu
66,191 Points

Ya, I specifically asked you :)

Aaron Elliott
Aaron Elliott
11,738 Points

Thanks Devin! I appreciate it, and I hope this helps. PHP is a fun language; Especially when you start looking into how to use it with AJAX.

Devin Scheu
Devin Scheu
66,191 Points

Hey arron, I have another problem that it would be helpful if you could help me solve.

Question: PalprimeChecker objects have a property called number. This task has two parts. First, assign that property a value of 17. Second, remove the two hash signs (##) in the first echo statement and instead concatenate the value of this number property. (Be sure to preserve the space after the two hash signs by concatenating a space after the number.)

My Code:

include "class.palprimechecker.php";

$checker = new PalprimeChecker(17);

echo "The number " + echo $checker;

echo "(is|is not)";

echo " a palprime.";

Aaron Elliott
Aaron Elliott
11,738 Points

Hey Devin,

Can you please open a new forum question for your new question?

Thanks!