In PHP, a recursive function refers to a self-calling function, that is, the function calls itself directly or indirectly within the function body; when using a recursive function, a judgment condition needs to be added to the function body to Determine whether the recursive call needs to continue. When the conditions are met, the recursive call of the function will be terminated.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
Recursive functions are self-calling functions, that is, functions Call yourself directly or indirectly within the function body. It should be noted that when using a recursive function, a judgment condition is usually attached to the function body to determine whether the recursive call needs to continue. When the condition is met, the recursive call of the function will be terminated.
The biggest advantage of recursive functions is that they can simplify complex and repetitive 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, although efficient, are difficult to program and relatively unreadable.
Recursive Thought
The main idea of recursion is to transform a relatively complex problem (original problem) into problems that are similar to the original problem. Solve smaller-scale problems (sub-problems). When each small problem is solved, the final big problem will naturally be solved.
The recursive method only needs a small amount of programs to describe the multiple repeated calculations required in the problem-solving process, greatly reducing the amount of program code. Of course, recursive functions are not perfect, and they also have certain shortcomings, that is, the recursive method functions do not operate efficiently.
The maximum number of recursion levels in PHP is not unlimited. This is related to the memory limit of the program. PHP5 allows a program to use 128M of memory by default, so when the number of recursion levels is too large, the 128M memory is exhausted. , the program will generate a fatal error and exit. PHP7 allows 256M of memory by default.
The maximum memory allowed by PHP can be modified by modifying the php.ini file as follows:
; Maximum amount of memory a script may consume (128MB) ; http://php.net/memory-limit memory_limit=256M
Recursion in PHP
To achieve recursion, the following two conditions need to be met:
The sub-problem must be the same thing as the original problem and simpler.
You cannot call yourself without limit, there must be an exit, and it can be simplified to non-recursive situation processing.
Example 1: Use a recursive function to implement the factorial of n!
<?php header('content-type:text/html;charset=utf-8'); function factorial($num){ //确定递归函数的出口 if($num == 1){ return 1; }else{ return $num*factorial($num - 1); } } echo '15 的阶乘是:'.factorial(15); ?>
The running results are as follows:
Example 2: Calculate the Fibonacci sequence.
The Fibonacci sequence is as follows:
1, 1, 2, 3, 5, 8, 13, 21, 34,...
The implementation code is as follows:
<?php header('content-type:text/html;charset=utf-8'); function demo($num){ // if($num == 1 || $num == 2){ return 1; }else{ return demo($num - 1) + demo($num - 2); } } echo '数列第 10 位是:'.demo(10); ?>
The running results are as follows:
##Recommended learning: "PHP Video Tutorial 》
The above is the detailed content of What does recursive function mean in php. For more information, please follow other related articles on the PHP Chinese website!