Home >Backend Development >PHP Tutorial >php递归,两个例子让你掌握PHP递归

php递归,两个例子让你掌握PHP递归

WBOY
WBOYOriginal
2016-06-23 13:22:58793browse

认真理解下面两个例子,就可以掌握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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn