Home >Backend Development >PHP Tutorial >What is PHP recursive function and simple example explanation

What is PHP recursive function and simple example explanation

伊谢尔伦
伊谢尔伦Original
2017-05-15 13:02:178204browse

What is php recursive function?

Recursive function is a self-calling function, which calls itself directly or indirectly within the function body, that is, the nested call of the function is the function itself. Usually, a conditional judgment statement is attached to this type of function to determine whether a recursive call needs to be executed, and the recursive calling action of the function is terminated under specific conditions, and the control of the current process is returned to the previous layer. function to execute. Therefore, when a function that performs a recursive call does not have additional conditional judgment statements, an infinite loop error may occur.

The biggest advantage of recursive function calling is that it can simplify complex repeated calling procedures in the program, and can use this feature to perform some more complex operations. For example, operations such as lists, dynamic tree menus, and directory traversal. The corresponding non-recursive functions, while efficient, are difficult to program and relatively unreadable. The goal of modern programming is primarily readability. With the continuous improvement of computer hardware performance, programs prioritize readability rather than efficiency in more situations. Therefore, the use of recursive functions is encouraged to implement program ideas.

A simple recursive call example is as follows:

<?php
 //声明一个函数,用于测试递归
 function test($n){
   echo $n." ";        //在函数开始输出参数的值
   if($n>0){                //判断参数是否大于0
     test($n-1);            //如果参数大于0则调用自己,并将参数减1后再次传入
   }else{                   //判断参数是不大于0
     echo "<-------->  ";
   }
   echo $n." ";
 }
 test(10);                   //调用test函数将整数10传给参数
?>

After the program is executed, the following results are output:

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

Find the reason why the numbers in the second half of the result are output in forward order

Explanation: In the above example, a test() function is declared, which requires an integer parameter. Call the test() function outside the function by passing the integer 10 as argument. In the test() function body, the first code outputs the value of the parameter and a space. Then determine whether the condition is true, and if so, call yourself and reduce the parameter by 1 and pass it in again. When it starts to be called, it calls the outer layer to the inner layer, and the inner layer calls to the inner layer until the innermost layer must end due to conditions not allowing it. When the most memory is finished, 85035d4d2c2d63d3f587aee1c9d8129e is output as a delimiter. The code after executing the call outputs the value of the parameter and a space, and it will return to the outer layer to continue execution. When the outer layer ends, return to the outer layer and continue execution, pushing out layer by layer until the outermost layer ends. The result after the execution is completed is the result we saw above.

The above is the explanation of PHP recursive functions and simple examples. In the next chapter, we will introduce Three methods of implementing recursive functions in PHP.

【Recommended related tutorials】

1. "php.cn Dugu Jiujian (4)-php video tutorial"

2. php programming from entry to mastering a full set of video tutorials

3. php practical video tutorial

The above is the detailed content of What is PHP recursive function and simple example explanation. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn