Understanding of php recursive code

Source: Internet
Author: User
{Code ...} the output result is as follows: {code ...} my doubt is that when $ count is added to 10, it will not call itself, then it will run the following code $ count -- and the output will end, but why is it still running nine times.
function test(){    static $count = 0;    $count++;    echo "-- ".$count." --\n";    if ($count < 10) {        test();    }    $count--;    echo "## ".$count." ##\n";}test();

The output is as follows:

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

My question is when$countAfter adding 10, it will not call itself, so it will run the following code$count--Then the output ends, but why is it still running nine times.

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

The output is as follows:

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

My question is when$countAfter adding 10, it will not call itself, so it will run the following code$count--Then the output ends, but why is it still running nine times.

Let's take a look at the entire execution process when $ count is less than 2:

  1. Change less than 10 to less than 2:

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

2. Replace the recursive test () in the lower than 2 with the code in the function body:

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. After test () is called

test()

4. The detailed execution process is as follows:

4.1 for the first time, line 1: $ count = 0; 4.2 After line 2 is executed, $ count = 1; 4.3 will output in line 3: ** -- 1 -- ** 4.4 and then execute line 4. Because $ count <2 is true; 4.5, line 6 will output the result after line5 is executed and changed to 2 4.6: ** -- 2 -- ** 4.7 then goes to line 7. Because $ count = 2, after line 8 4.8 line 8 is executed directly at no less than 2, $ count becomes 1, then execute line 9 4.9 in line 9 and the output will be: ** #1 # ** 4.10 then execute line 10, $ count minus 1, the value is 0 4.11. Therefore, the output is ** #0 # ** In line 11 ##**

5. Conclusion:
When we assume that $ count is less than 2, we can see in the detailed execution process above:
Test () is executed twice in total,
Output result:
-- 1 --, -- 2 --, #1 ##,## 0 ##
At this time, we will try again to reduce $ count to 10, which will easily explain the landlord's questions.
I don't know if it helped you. Contact us if you have any questions.

No more than 9 times,testThe function runs only 10 times. However, each function has two outputs, so a total of 20 outputs are performed.

If there is no return, it will not stop, and it will continue to be executed after it is called.

Because when$count= 10, the code is still executed. You can write it like this.

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

@ Phping: This is my understanding of recursion. It is similar to yours.

Function test () {static $ count = 0; // initialize the static variable $ count ++; // $ count = 1; echo "--". $ count. "-- \ n"; // output 1 if ($ count <3) {// 1 less than 3 is true static $ count = 0; // initial $ count ++; // $ count = 2; echo "--". $ count. "-- \ n"; // output 2 if ($ count <3) {// 2 less than 3 is true static $ count = 0; // initial $ count ++; // $ count = 3 echo "--". $ count. "-- \ n"; // output 3 if ($ count <3) {}// 3 not less than 3 is false $ count --; // $ count = 2; echo "##". $ count. "##\ n"; // output after subtraction 2} $ count --; // $ count = 1 echo "##". $ count. "##\ n"; // output after subtraction 1} $ count --; // $ count = 0; echo "##". $ count. "##\ n"; // output 0} test () after subtraction ();

Final output

-- 1 ---- 2 ---- 3 --## 2 #### 1 #### 0 ##

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.