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 trialChloe Conn
3,217 PointsHELP PLEASE
What am I doing wrong?
<?php
$palindromic_primes = array(11, 757, 16361); $sum; mimic_array_sum();
function mimic_array_sum($array){ $sum = 0; foreach ($array as $element){ $sum = $element + $sum; } return $sum; }
?>
5 Answers
Josh Page
1,928 PointsHi Chloe This should work '''
<?php
$palindromic_primes = array(11, 757, 16361);
$sum;
mimic_array_sum($palindromic_primes);
function mimic_array_sum($array){
$sum = 0;
foreach ($array as $element){
$sum = $element + $sum;
}
echo $sum;
}
?>
The problem was you called the function on line 3 (of my code) but didn't put anything inside the brackets leading to errors when the function ran and tried to use the value from brackets. This meant the for each didnt work as the array value was blank.
If you need anymore explanation of the problem feel free to ask :) Josh
Michael Alaev
5,415 PointsYou should pass in the $palindromic_primes inside your function.
mimic_array_sum($palindromic_primes);
Chloe Conn
3,217 PointsNeither one of your advice solved it.....
Josh Page
1,928 PointsWhat are you trying to do? The code i submitted runs fine and produces an output of 17129 (Which is definatly right as I checked with a calculator.)
Can you send me your whole code (HTML and Everything) please
Chloe Conn
3,217 PointsI did and it doesn't work =(