首页 > 代码库 > 递归 求1+2+3+....+10

递归 求1+2+3+....+10

# 递归 求1+2+3+....+10
function sum($total = 0,$num){
    $total += $num;
    if($num == 1) {
        return $total;
    }else{
        return sum($total,$num -1);
    }
}
echo sum(0,10);

result:55

递归 求1+2+3+....+10