递归函数

//recursion 递归
function test($i){
    echo $i,'<br/>';   //3
    --$i;            
    if($i>=0){
        test($i);  //test(2)
        /*
            echo $i,'<br/>';   //2
                --$i;            
                if($i>=0){
                test($i);  //test(1)
                }
                echo $i,'<br/>';   //1
                --$i;            
                if($i>=0){
                test($i);  //test(0)
                }
                echo $i,'<br/>';   //0
                --$i;            
                if($i>=0){
                test($i);  //test(-1)
                }
        */
    }

}
test(3);
echo '<hr/>';

function test1($i){
    echo $i,'<br/>'; //3
    if($i>=0){
        test1($i-1);  //test(2)
        /*
            echo $i,'<br/>'; //2
            if($i>=0){
                test1($i-1);  //test(1)
                echo $i,'<br/>'; //1
                if($i>=0){
                    test1($i-1);  //test(0)
                    echo $i,'<br/>'; //0
                    if($i>=0){
                        test1($i-1);  //test(-1)
                        echo $i,'<br/>'; //-1
                        if($i>=0){
                            test1($i-1);  //条件为假,不执行
                        }
                        echo $i,'<br/>'; //-1
                    }
                    echo $i,'<br/>'; //0
                }
                echo $i,'<br/>'; //1
            }
            echo $i,'<br/>'; //2    
        */
    }
    echo $i,'<br/>'; //3
}
test1(3);


function test2($i){
    echo $i,'<br/>';
    if($i>=0){
        //test2($i-1);
        $func=__FUNCTION__;
        $func($i-1);
    }
    echo $i,'<br/>';
}
echo '<hr/>';
test2(3);

posted @ 2017-05-04 10:13  皇家玄学团  阅读(103)  评论(0编辑  收藏  举报