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

WBOY
Release: 2016-06-23 13:22:58
Original
763 people have browsed it

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

返回结果:
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;
Copy after login

返回结果:
a2
a1
b0
c1
c2
2

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template