求php一段遞迴代碼的理解

來源:互聯網
上載者:User
關鍵字 php
function test(){    static $count = 0;    $count++;    echo "-- ".$count." --\n";    if ($count < 10) {        test();    }    $count--;    echo "## ".$count." ##\n";}test();

結果輸出如下:

-- 1 ---- 2 ---- 3 ---- 4 ---- 5 ---- 6 ---- 7 ---- 8 ---- 9 ---- 10 --## 9 #### 8 #### 7 #### 6 #### 5 #### 4 #### 3 #### 2 #### 1 #### 0 ##

我的疑惑是 當$count加到10後就不會再調用自身了,那麼它會運行下面的代碼$count--然後輸出就結束了,可是為什麼它還是運行了9次呢,求高手解答。

回複內容:

function test(){    static $count = 0;    $count++;    echo "-- ".$count." --\n";    if ($count < 10) {        test();    }    $count--;    echo "## ".$count." ##\n";}test();

結果輸出如下:

-- 1 ---- 2 ---- 3 ---- 4 ---- 5 ---- 6 ---- 7 ---- 8 ---- 9 ---- 10 --## 9 #### 8 #### 7 #### 6 #### 5 #### 4 #### 3 #### 2 #### 1 #### 0 ##

我的疑惑是 當$count加到10後就不會再調用自身了,那麼它會運行下面的代碼$count--然後輸出就結束了,可是為什麼它還是運行了9次呢,求高手解答。

這樣吧,我們假設讓$count小於2時來看看整個執行過程:

  1. 將小於10改為小於2:

 function test()    {        static $count = 0;            $count++;        echo "-- ".$count." --\n";        if ($count < 2) {            test();        }        $count--;        echo "## ".$count." ##\n";    }

2.將小於2時,裡面遞迴的test()換成函數體內的代碼:

function test(){    static $count = 0;   // line 1    $count++; // line 2    echo "-- ".$count." --\n"; // line 3    if ($count < 2) { // line4        static $count = 0;        $count++; // line 5        echo "-- ".$count." --\n";// line 6        if ($count < 2) {// line 7            test();        }        $count--;// line 8        echo "## ".$count." ##\n";// line 9    }    $count--;// line 10    echo "## ".$count." ##\n";// line 11    }

3.調用 test()後

test()

4.來看整個詳細的執行流程:

 4.1 第一次,line 1:$count = 0; 4.2 執行line 2後,$count = 1; 4.3 所以在line 3 會輸出: **-- 1 --** 4.4 接著執行line 4,由於現在 $count < 2為真; 4.5 所以執行 line5後變為2 4.6 所以 line 6 會輸出: **-- 2 --** 4.7 然後到 line 7,由於 $count = 2,不小於2直接執行line 8 4.8 line 8 執行後, $count變為1,接著執行line 9 4.9 在line 9 會輸出: **## 1 ##** 4.10 接著就執行 line 10,$count再減1,就變為0 4.11 因此在 line 11會輸出: **## 0 ##**

5.總結:
當我們假定 $count小於2時,在上面詳細的執行流程中我們看到:
test()總共被執行了2次,
輸出結果為:
-- 1 --,-- 2 --,## 1 ##,## 0 ##
這時候再回頭將 $count小於10,就容易解釋樓主的疑問了。
不知道是否協助到了你。有疑問繼續聯絡。

沒有再運行9次呀,test函數只運行了10次。但是每個函數中有兩個輸出,所以總共輸出了20次。

有沒有return不會停止,在調用他自己後還會繼續執行的

因為當$count=10的時候,該段代碼還是執行了,你可以這樣寫

function test(){    static $count = 0;    $count++;    echo "-- ".$count." --
"; if ($count < 10) { test(); $count--; echo "## ".$count." ##
"; }}test();

@phping 這是我對於剛才遞迴的理解,和你的類似。

function test() {    static $count = 0; // 初始化靜態變數$count    $count++; // $count = 1;    echo "-- ".$count." --\n"; // 輸出 1    if($count < 3) { // 1 小於 3 為真        static $count = 0; // 初始        $count++; // $count = 2;        echo "-- ".$count." --\n"; // 輸出 2        if($count < 3) { // 2 小於 3 為真            static $count = 0; // 初始            $count++; // $count = 3            echo "-- ".$count." --\n"; // 輸出 3            if($count < 3) {} // 3 不小於 3 為假            $count--; // $count = 2;            echo "## ".$count." ##\n"; // 減後輸出 2        }        $count--; // $count = 1        echo "## ".$count." ##\n"; // 減後輸出 1    }    $count--; // $count = 0;    echo "## ".$count." ##\n"; // 減後輸出 0}test();

最後輸出

-- 1 ---- 2 ---- 3 --## 2 #### 1 #### 0 ##
  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.