PHP Recursive Function Example analysis

Source: Internet
Author: User
This paper

   Public Function Digui ($i)    {        if ($i = = ' 1 ') {            echo  "*";            echo "<br>";            return;        } else{            $this->digui ($i-1);            for ($j =1; $j <= $i; $j + +) {                echo  "*";            }            echo "<br>";        }    } Call $this->digui (3), result ******  function Digui2 ($n) {        echo $n. ";        if ($n >0) {            $this, Digui2 ($n-1);        } else{            echo "<-->";        }        echo $n. " ";    }

Call $this->digui2 (3);

Results

3 2 1 0 <-->0 1 2 3

Recursive functions perform anatomical examples (reproduced by others)

Look at the following code:

<?phpfunction One ($num) {echo $num; ($num-1); Echo $num;} function double ($num) {echo $num; three ($num-1); Echo $num;} Function three ($num) {echo $num;} One (3);? >

The above code to decompose the test () function, we think:
When executing the one (3) function, the same as the test () function, the first to output 3, and then call the two (2) function,
Note that at this time there is no output of the following 3,
and then go, Execute the two (2) function, output 2, call the three (1) function, again, there is no time to output the following 2,
Execute three (1), direct output 1, do not call other functions,
at this point, we would like to just two () function is not yet done, good, Then the two () function does not complete the part, after the two () function is finished, that is, the output of the following 2, and then start to execute the one () function is not finished, that is, the output of the following 3, when all functions are completed.
then the output is:
3 2 1 2 3

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.