In the previous article, we introduced to you the PHP algorithm series "PHP Random Picking Algorithm" and "PHP Bubble Sorting Algorithm》, friends in need can learn about it. In this article, we will continue to bring you common PHP algorithms, namelyPHP recursive algorithm.
#In the PHP development process, recursive algorithms are often used for infinite classification. So the so-called recursion is a mechanism in which a function calls itself. To put it simply, you call yourself directly or indirectly in the function body, but you need to set the conditions for self-calling. If the conditions are met, the function itself is called. If the conditions are not met, the self-calling of this function is terminated.
And there are many ways to implement recursive algorithms, such as through "static variables", "global variables", and "passing parameters by reference".
Below we will introduce to you one of the methods using specific code examples, which is the method of usingstatic variables!
The code is as follows:
Copy after login
Output:
0 1 2 3 4 5 6 7 8 9
In this method, we mainly use static to define static variables to implement recursive sorting. As above, we have defined a call method and static variable $i. If we do not add a judgment to the $i variable but run it directly, an infinite loop will occur. So we added an if conditional judgment statement here. Finally, the loop calls its own method.
This article is an introduction to the use of static variables to implement PHP recursive algorithms. In subsequent articles, we will continue to introduce the relevant implementation methods of PHP recursive algorithms.
The above is the detailed content of PHP recursive algorithm (1). For more information, please follow other related articles on the PHP Chinese website!