The running result of this recursive function is not clear.

<?php
function recursion(){
    static $count = 0;
    $countPP;
    echo $count."  ";
    if($count < 10){
        recursion();
    }
    echo $count."  ";
    $count--;
}
    recursion();
?>

Why is the running result

?

1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1

1. Why not
1 2 3 4 5 6 7 8 9 10 10
2. What are its exit conditions?

Php
Jul.07,2021

1. Because PHP is executed by Synchronize, if you satisfy the condition and execute recursion () recursively, it is tantamount to being nested all the time. Then Synchronize execution is naturally
1 2 3 4 5 6 7 7 8 9 10 9 8 7 6 5 4 3 2 1
you can change one of them into the other and you can see it intuitively
2. When $count = 10, it naturally exits and does not perform recursion, so there will be


recursive nesting layer after layer. When the last layer $count is 10:00, execution stops, and the output is 1 2 3 4 5 6 7 8 9 10 , but the lower echo $count. "; has not been output yet.
then start outputting echo at the bottom, and output layer by layer until the first layer, that is, the layer 1 after $countPP : 10 9 8 7 6 5 4 3 21 ;
the program exits.


you have just entered the if judgment, but the function has not been executed yet, so when the judgment is finished, you will start to execute the later part of the function, and the functions in if will also be executed, so you will then output


We can simplify the code to help you understand recursion. PHP is executed sequentially, and recursion can be simply understood as copying its own code again.
because 10 times is too much, let's change it to 3 times

<?php
function recursion(){
    static $count = 0;
    $countPP;                    //$count = 1;
    echo $count."  ";      //echo 1
    if($count < 3){
        $countPP;                //$count = 2
        echo $count."  ";  //echo 2
        if($count < 3){
            $countPP;                //$count = 3
            echo $count."  ";  //echo 3
            if($count < 3){          //if
                //...
            }                        //echo 3
            echo $count."  ";
            $count--;                //$count =2
        }
        echo $count."  ";      //echo 2
        $count--;                    //$count =1
    }
    echo $count."  ";          //echo 1
    $count--;                        //$count =0
}
    recursion();
?>

then we change the call itself in the body of the function into its own code. You will find that it is very simple and has been carried out step by step.


personally, I think it may be easier to change the above code to this. If it is a step-by-step analysis, someone else has already provided the answer. However, generally speaking, writing recursion or analyzing recursion does not use step-by-step analysis, because the complexity of the function is difficult to analyze.

here's how to understand recursion and use recursion https://acl.readthedocs.io/en..
you can also see the iteration and recursion section of Chapter 1 of SICP.

<?php
function recursion($count){
    $countPP;
    echo $count." ";
    if($count < 10){
        recursion($count);
    }
    echo $count." ";
    $count--;
}

recursion(0);
?>
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b37786-2b8eb.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b37786-2b8eb.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?