PHP Recursive algorithm simplification

Source: Internet
Author: User
Recursive function for the self-invocation function, in the function body directly or directly from a call from a, but the requirements set from the condition of the call, if the satisfaction of the condition, then call the function itself, if not satisfied to stop the function of the self-invocation, and then the current flow of the main control to return to a layer of function to perform, perhaps so

For example, code:

function test ($n) {    echo $n. "  ";    if ($n >0) {        Test ($n-1);    } else{        echo "";    }    echo $n. "  "} Test (2)

For example, the output is 2 1 0<–>0 1 2

I'll explain why the output is so.

The first step is to perform test (2), Echo 2, and then, due to 2>0, perform test (1), and back there is no time to perform the Echo 2
Step two, perform test (1), Echo 1, and then because of 1>0, perform test (0), the same back there is no time to perform the Echo 1
The third step, fulfilling test (0), Echo 0, fulfilling test (0), Echo 0, is not satisfied with the condition of 0>0 at the moment, not fulfilling the test () function, but echo "", and performing echo 0 behind

Now the function is no longer called from the beginning of the process of the main control handed back to the previous layer of function to perform, that is, the beginning to fulfill just all test () function did not have time to output the final one echo,0 layer is 1 is the output 1 1 of the previous layer is 2 is also the Output 2 2 there is no mountain layer so? The content of the output is 2 1 0<–>0 1 2

Related reading:

PHP recursive algorithm

PHP recursive algorithm: PHP recursive algorithm php tree-do not need recursion:/*** Create parent node tree Array * parameter * $ar array ...

PHP Recursive algorithm PHP interview common algorithm

PHP recursive algorithm: PHP recursive algorithm PHP interview common algorithm ...

A study of PHP recursive algorithm based on PHP red envelopes algorithm

PHP recursive algorithm: PHP recursive algorithm based on the algorithm of PHP red envelopes: The whim of the alumni group of red Envelopes, I set the total amount of red envelopes 10 yuan, support 28 people randomly pick. ......

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.