function recsum($a, $index) { if($index>0) { $total=$a[$index]+recsum($a,$index-1); } else { $total=$a[$index]; } return $total; } $array=array(1,2,3,4,5); $sum=recsum($array,4); echo $sum;