函数递归,方法一
$num=1;
function test($num){
$num++;
if($num<5){
echo $num;
test($num);//递归,满足条件调用函数
}else{
return;
}
}
test($num);//把$num当做参数传到函数里面去
用递归处理阶乘问题
function factorial($num){
if($num>0){
return $num*factorial($num-1);
}else{
return 1;}}
echo factorial(5);//调用,打印
9.函数递归,方法二,静态变量
静态变量(每次调用完成后会记住上一次的值),也叫常驻内存,关键字是static
function test(){
static $num=1;
$num++;
if($num<5){
echo $num;
test();
}else{
return;}}
test();
网友评论