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

Linda Woo
Linda Woo
2,018 Points

A hint?

Am I going to create a function within a function? One function will allow me to cycle through the array - something like the mimic_count function in the video because I can't just assume the array will have three integers like the palindromic_primes array in the challenge. Then will I use this function to cycle through each number in the arrays? I will add $array[$count] + $array[... hmmm actually that won't work. As you can see, I am having a problem with this challenge.

Linda Woo
Linda Woo
2,018 Points

Here's where I landed, and I know it's wrong....any hints?

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

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

1 Answer

Hi Linda,

With php's foreach loop you don't need a "count" variable. The foreach loop knows how many items are in the array. You're over-complicating things. Study the code below.

Jeff

<?php

function mimic_array_sum($array) {

  $sum = 0;

  foreach ($array as $num); {
    $sum = $sum + $num;
  }
    return $sum; 
}

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

$total = mimic_array_sum($palindromic_primes);

echo $total;

?>
Linda Woo
Linda Woo
2,018 Points

Thank you, thank you!