Php recursion problems

Source: Internet
Author: User
Php recursion PHPcodefunctiontest ($ a) {echo '& amp; nbsp'. $ a. '& amp; nbsp'; if ($ a & gt; 0) {php recursion problem
PHP code
              function test($a)    {        echo ' '.$a.' ';        if($a >0)        {                        test($a-1);                }else        {            echo '<--->';        }        echo ''.$a.'';    }    test(3)



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

You can explain the operation steps, or click the reference materials for me to study, I am a newbie. Forgive me

------ Solution --------------------
Brother, a word is hard to do, call a function, you have to open up a new stack, recursive call test ($ A-1) end, has output 3 2 1 0 This understand?
Then, of course, it is <---> This is the thing, right? do you understand?
At last, we need to execute this echo to reclaim the opened stack' '. $ .'';~~~
I don't understand where it is. I 'd like to explain it to you as much as I can ~~

------ Solution --------------------
Each layer is the same:

1. print $
2. go to the next layer for recursion or printing. <--->
3. print $

That is:

Print 3, print 2, print 1, print 0, Print <--->, print 0, print 1, print 2, print 3.

Red is Step 1, Blue is step 2.


------ Solution --------------------
Test (3) run function test:
1. echo $ a = 3 and enter if to Judge 3> 0 to execute test (2); here the function test (3) has not been completed yet. Is to execute test (2) ------- * 1;
2. test (2) echo $ a = 2 and then judge 2> 0 to execute test (1); here test (2) is not completed yet. First run test (1) ------------ * 2;
3. test (1) echo $ a = 1; then judge 1> 0 to execute test (0); here test (1) has not been completed. First run test (0) ----------- * 3;
4. test (0) echo $ a = 0; then, judge that 0> 0 has executed echo <---> echo 0;

5. 4. after the execution is complete, return to * 3. after the judgment is complete, execute echo $ a = 1;
6. after the execution is complete, return to * 2 echo $ a = 2;
7. 6. after execution, return to * 1 echo $ a = 3;

This is the depth of a layer, and then the exit of a layer.

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.