PHP custom function recursive function
Recursive function, recursion is just a name, and the provisions of the recursive function: the function body calls the function itself.
This requires a certain depth of thinking and understanding. During the learning process of this chapter, if you really cannot keep up with your thinking, you can skip this chapter without studying.
Because, in actual work, recursion is useful, but the usage will not be very large. Recursion is mainly used in actual work: when operating files and folders.
Solution:
In case your thinking cannot keep up with this chapter, you can directly understand the principles of this block and use ready-made file and folder processing functions or file processing classes.
Let me talk about a few blind spots in thinking:
1. The code is executed from top to bottom. All codes do not have stop characters such as exit, and the function must be completed.
2. If the function jumps from function A to function B, function B must be executed before executing the remaining code of function A.
3. The recursive function must be able to complete execution and have an end condition, otherwise the function will enter an infinite loop. The function will execute itself forever.
Let’s write a code to understand it:
<?php $num = 10; //调用一次函数A(); A($num); function A( $arg ){ echo $arg; //在函数A里面去,跑去执行函数B去了 B($arg); echo '我们需要不断的努力,努力到上天都为我们感动'; echo $arg.'<br />'; } function B( $number ){ echo $number; echo '俺是狗蛋,执行完了<br />'; } ?>
Through the above example, you will find:
1. Halfway through the execution of function A, run to execute it Function B
2. After executing function B, the first thing displayed is: "I am a dog, the execution is over", and then the next thing displayed is: "We need to continue to work hard, and work hard until God is moved by us. ”
3. This proves what we call the blind spot of thinking. The code is executed from top to bottom, and the code must be executed to completion.
Let’s write a simple recursive code and let the function call itself.
<?php $n = 2; function dg( $n ){ echo $n.'<br />'; $n = $n - 1; if($n > 0){ //在函数体内调用了dg自己哟 dg($n); }else{ echo '--------------'; } echo '俺是狗蛋,俺还没执行' . $n . '<br />'; } ?>
Guess what the displayed result is? why is it like this?
Let’s reason carefully:
1. The first time dg() is called, the number $n = 2 is passed to dg, and 2 is displayed first
2. Then set the value of $n - 1 $n to 1
3. Then judge whether $n is greater than 0, it must be greater than 0, so call the recursion yourself and execute yourself again.
4. When executing your own dg() for the second time, the bottom echo 'I am a dog, I haven't executed it yet'. $n. '
'; has not been executed yet. arrive. Wait for execution to complete before executing again
5.$n is equal to 1 at this time, so 1 is displayed.
6.$n decrements itself once, and the result of $n is 0
7.$n is definitely not true if it is greater than 0, so a message is displayed: "------ --------"
8. At this time it's time to execute: echo 'I'm a bitch, I haven't executed it yet' . $n . '
';
9. The second execution of dg() is completed. The first dg() code has not been executed yet, so the remaining code in point 4 is executed.