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. ......