<?php
/**
*php递归函数细节
*从1到5的阶乘
* */
header("Content-Type:text/html;charset=utf-8"); function sum($num){
$tot = $num;
echo "进入第{$num}层函数<hr style='border-style: dotted' />";
if($num > 1){
$tag = true;
$sum = sum(--$num); /*$sum = sum(--$num)之前是进入函数,之后是函数退出*/
echo "<span style='color: red'>(\$tot => {$tot}) x (\$sum => {$sum}) <b>=</b> </span>";
$tot *= $sum;
} echo "<span style='color: red'>(\$tot => {$tot})</span><br />";
if($tag){
++$num;
echo "退出第{$num}层函数<hr />";
}else{
echo "退出第{$num}层函数<hr />";
} return $tot;
}
echo "等于:" . sum(5);
以上代码执行后的效果如下: