Am a php developer and i have studied about recursive function and i coded to find the sum of elements in an array with recursive function but i got the error.
THE CODE WHICH I GOT FROM INTERNET
$example = array(10,20,30);
function sum_array($array) {
$total = 0;
foreach ($array as $element) {
if(is_array($element)) {
$total += sum_array($element);
} else {
$total += $element;
}
}
return $total;
}
echo sum_array($example); // Outputs 60
MY CODE
<?php
$example = array(10,20,30);
function add_me($arr) {
if($arr==0) {
return $arr;
}
return add_me($arr[0]+$arr[1]+$arr[2]);
}
The first code which i found from the internet works well but in case of my code it gives an error. When i called it using echo add_me($example);
it forms an error .
Can you please tell me why it is like that ..Any help would be appreciated ..Thanks.