Home > Daily Programming > PHP Knowledge > PHP recursive algorithm (3)

PHP recursive algorithm (3)

藏色散人
Release: 2021-01-11 10:46:37
Original
13929 people have browsed it

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.

PHP recursive algorithm (3)

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());
Copy after login

Output $a:

PHP recursive algorithm (3)

##Output $result:

PHP recursive algorithm (3)

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template