Home >Backend Development >PHP Tutorial >php递归,两个例子让你掌握PHP递归
认真理解下面两个例子,就可以掌握PHP递归了
function test($n){ if($n>0){ echo 'a'.$n."\n"; $n = test($n-1); }else{ echo 'b'.$n."\n"; return $n; } echo 'c'.$n."\n"; return $n;}echo test(2);exit;
返回结果:
a2
a1
b0
c0
c0
0
华丽分割线--------------------------------
function test($n){ if($n>0){ echo 'a'.$n."\n"; test($n-1); }else{ echo 'b'.$n."\n"; return $n; } echo 'c'.$n."\n"; return $n;}echo test(2);exit;
返回结果:
a2
a1
b0
c1
c2
2