Recursive functions, recursion is just a name, and the recursive function is specified: the function itself calls the function itself.

This requires a certain depth of thinking. In the course of this chapter, if you really have a place where your thoughts can't keep up, you can skip this chapter and don't have to learn.

Because, in actual work, recursive use is useful, but the amount of use is not large. Recursion is mainly used in actual work: when files and folders are used.

Solution: In 
case your mind can't keep up with this chapter, you can directly understand the principle of this block, and use the ready-made file and folder processing function or file processing class.

I said a few blind spots in thinking:

    1. The code is executed from top to bottom. All code does not have a stop character such as exit. The function must be executed.

    2. If the function jumps from function A to function B, the function B must be executed and the remaining code of function A must be executed.

    3. The recursive function must be able to execute the end condition, otherwise the function will be limited to an infinite loop. The function will continue to self-execute.

Let's write a code to understand:


$num = 10;

/ / Call the function A ();

Function A( $arg ){

   Echo $arg;

   / / In the function A, go to the function B to go

   Echo 'We need constant efforts and strive to be moved to us in heaven';

   Echo $arg.'<br />';

Function B( $number ){

       Echo $number;

       Echo '俺 is a dog egg, finished <br />';


Through the above example, you will find:

    1. When the function A is halfway through, the function B is executed.

    2. After executing function B, the first thing to display is: "Oh is the dog's egg, the execution is finished", and then it is displayed: "We need constant efforts and strive to be moved to us."

    3. That is to prove the content inside the blind zone of the thinking, the code is executed from top to bottom, and the code must be executed.

    Let's write a simple recursive code that lets the function call itself.


$n = 2;

Function dg( $n ){

   Echo $n.'<br />';

   $n = $n - 1;

   If($n > 0){
       / / In the body of the function called dg yourself 哟


       Echo '--------------';

   Echo '俺 is a dog egg, I haven't executed yet. ' $n . '<br />';

Guess what is the result? why is it like this?

Let's take a closer look at it:

    1. The first call to dg(), the number $n = 2 is passed to dg, first displayed 2

    2. Then put the value of $n - 1 $n to 1

    3. Then determine if $n is greater than 0, which is definitely greater than 0, so call recursive yourself and execute it yourself.

    4. The second time I executed my own dg(), and the bottom echo '俺 is a dog egg, I haven't executed yet. ' $n . ' 
'; Not yet implemented. Wait for execution to complete before executing

    5. $n is equal to 1 at this time, so 1 is displayed.

    6. $n has reduced himself once, and the result of $n is 0.

    7.$n is greater than 0 and certainly does not hold, so it shows one: "--------------"

    8. At this time it should be executed: echo '俺 is a dog egg, 俺 has not been executed yet. ' $n . ' 

    9. The second execution of dg() is completed. The code for the first time dg() has not been executed yet, and the remaining code in point 4 is executed.

Last Updated: 2018-07-06 12:32:44 By akshay


share_facebook share_twitter share_google_plus

Please Comment Here :