Home >Daily Programming >PHP Knowledge >PHP recursive algorithm (3)
In "PHP Recursive Algorithm (1)" and "PHP Recursive Algorithm (2)", we introduced how to use static variables and global variables respectively. Implement recursive algorithms. In this article, we will continue to explain how to achieve this through referencing and passing parameters.
Below we will introduce specific code examples to introduce the recursive algorithm through passing parameters by reference.
The code example is as follows:
<?php function test($a=0,&$result=array()){ $a++; if ($a<10){ $result[]=$a; test($a,$result); } echo $a."<hr>"; return $result; } var_dump(test());
Output $a:
##Output $result: The reference of php is simply to add the & symbol (that is, the reference symbol) in front of the variable, function, object, etc. Here you need to understand the concept of PHP reference transfer, that is, you can pass a variable by reference Passed to a function so that the function can modify the value of its argument. Quotation in PHP means that different names access the same variable content. Here we add an & symbol before $result, and finally output the $result result as shown in the figure above. In subsequent articles, we will use xdebug to demonstrate the implementation process of this method. This article is an introduction to the method of implementing recursive algorithms through PHP reference parameter passing. It is simple and easy to understand. I hope it will be helpful to friends in need!The above is the detailed content of PHP recursive algorithm (3). For more information, please follow other related articles on the PHP Chinese website!