On PHP Fourth---recursive function _php tutorial

Source: Internet
Author: User
Many students will feel dizzy when learning the recursive function, unable to understand the principle of recursive function and operating mechanism, this article will give you a detailed explanation of the operation mechanism and application of recursive function.

So what is a recursive function?

Recursive functions are self-calling functions, in the function of the body directly or indirectly call themselves, but need to set the self-invocation of the condition, if the condition is satisfied, then call the function itself, if not satisfied then terminate the function of the self-invocation, and then the current process of the main control to return to the previous layer of function to execute, it may be

Well, then we'll take Gaulo's teacher's "detail PHP" example to explain to you.

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


Let's start by thinking, what is the final output of this example?

OK, let's take a look at the result of this function output:

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

How, do not know whether this result is the same as everybody thought?

OK, let me take a step-by-step to explain to you ...

The first step is to execute Test (10), Echo, and then because of 10>0, execute test (9), followed by Echo 10

The second step, execute test (9), Echo 9, and then because of 9>0, execute test (8), also back there is no time to execute the Echo 9

The third step, execute Test (8), Echo 8, and then because of 8>0, execute test (7), also back there is no time to execute the Echo 8

The fourth step, execute test (7), Echo 7, and then because 7>0, execute test (6), also back there is no time to execute the Echo 7

The fifth step, execute test (6), Echo 6, and then because 6>0, execute test (5), also back there is no time to execute the Echo 6

...........

Tenth step, execute test (0), Echo 0, at this time the condition of 0>0 is not satisfied, do not execute the test () function, but echo "<-->", and perform the following echo 0

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


At this point, the contents of the output, such as the red part shown above, the function has no longer called itself, began to return the master control of the process to the previous layer of function to execute

That is to start executing the last echo that just all the test () functions did not have time to output

Its flow is like this:


In the first to tenth step of the function execution, the function output is the green part, the red part also "does not come" output, the call itself to perform the operation, and so on, until the process execution to no longer meet the call itself, the output of "<-->", at this time, the process to execute the previous "did not come" output code.


Just like the game we play, Kill a monster, fall out of a baby, but there are other monsters waiting for you to destroy, you have to destroy all the monsters to come back to a one to pick up the baby.


How about, such a kind of with everyone to explain is it clear?
Some students will ask, I finished all the test function, the final output 0


That is, the output here,


9 8 7 6 5 4 3 2 1 0 <--> 0


So why is the next output 1, not 10?


For this question, to help you understand, let me give you an example:


Look at the following code:

function One ($num) {
Echo $num;
($num-1);
Echo $num;
}
function ($num) {
Echo $num;
Three ($num-1);
Echo $num;
}
Function three ($num) {
Echo $num;
}
One (3);
?>

The above code decomposes the test () function, and we think:


When executing the one (3) function, the same as the test () function, first output 3, and then call the one (2) function,


Note that there is no output of the following 3 at this time,


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, not call other functions,


At this point, we would like to just the two () function is not finished, good, then execute the two () function is not complete, 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 part, that is, the output of the following 3, All functions are executed at this time.


Then, the output is:


3 2 1) 2 3


What, is it easier for people to understand this?


If you still do not understand, please ask questions in the bottom, please also give valuable suggestions, I will make corresponding improvements and explanations.


Excerpted from Zdrjlamp

http://www.bkjia.com/PHPjc/478280.html www.bkjia.com true http://www.bkjia.com/PHPjc/478280.html techarticle Many students will feel dizzy when learning the recursive function, unable to understand the principle of recursive function and operating mechanism, this article will give you a detailed explanation of the operation mechanism and application of recursive function. ...

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