Three basic methods of recursive implementation in PHP

Source: Internet
Author: User
This article mainly introduces three basic methods of recursive implementation of PHP, including using reference to do parameters, using global variables, using static variables to implement recursion, and attach the relevant examples, and finally give you a demo, involving PHP recursive operation skills, the need for friends can refer to the next

Using references to make parameters

First of all, regardless of the reference do not do parameters, you must first understand what is the reference? A reference simply refers to a variable of two different names pointing to the same storage address. Each variable has its own storage address, and the assignment deletes the lines. Now, two variables share a single storage address. $a =& $b;. In fact, $a regardless of their original storage address, do not want to share a room with the $b. Therefore, any change to the value of the stored address will affect both values.

Functions are inherently fragmentation, even for functions with the same name. The recursive function is to consider the reference as a parameter, to become a bridge, to form a data share between two functions. Although the two functions seem to operate on a different address, the actual operation is a memory address.

function test ($a =0,& $result =array ()) {$a ++;if ($a <10) {  $result []= $a;  Test ($a, $result);} echo $a; return $result;}

Take advantage of global variables

Using global variables to complete the recursive function, make sure you do understand what global variables are. Global declares that a variable is simply a reference to an external variable with the same name. The scope of the variable is still within the scope of this function. Changing the values of these variables, the value of the external variable with the same name has changed naturally. But once & is used, the variable with the same name is no longer a reference with the same name. Using global variables to implement recursive functions it is not necessary to understand such a deep layer, but also to maintain the original view of global variables can logically understand the recursive function.

function test ($a =0, $result =array ()) {  global $result;  $a + +;  if ($a <10) {    $result []= $a;    Test ($a, $result);  }  return $result;}

Using static variables

We often see static in classes, and today we use it in recursive functions. Remember the function of static: Initializes the variable only the first time it is called, and retains the value of the variable.

As an example:

function test () {static $count =0;echo $count; $count + +;} Test (); test (); test (); test (); test ();

What is the execution result of this section of code? Is it 00000? Not necessarily. Is 01234. First call test (), static initialization of the $count, after each execution will retain the value of $count, no longer initialized, the equivalent of directly ignoring the static $count = 0; This sentence.

Thus it is conceivable to apply static to the function of recursion. Variables that need to be "bridges" between recursive functions are initialized with static, and each recursion retains the value of the bridge variable.

function test ($a =0) {  static $result =array ();  $a + +;  if ($a <10) {    $result []= $a;    Test ($a);  }  return $result;}

Summarize

The so-called recursive function, the focus is how to deal with the function call itself is how to ensure that the required results can be reasonably "passed" between functions, of course, there is no need to pass the function between functions worthy of recursive functions, for example:

function test ($a =0) {  $a + +;  if ($a <10) {    echo $a;    Test ($a);}  }

In the face of such a function, we do not have to worry about the big headache. By the way, an in-depth understanding of variable reference knowledge is useful for solving such problems.

Finally to share a PHP implementation of recursive and infinite classification method, the specific implementation method is as follows:

<?phpecho "<pre>"; $area = array (' ID ' =>1, ' area ' = ' Beijing ', ' pid ' =>0), array (' ID ' =>2, ' area ' = > ' Guangxi ', ' pid ' =>0 ', array (' ID ' =>3, ' area ' = ' Guangdong ', ' pid ' =>0), array (' ID ' =>4, ' area ' = ' Fujian ', ' pid ' = >0), array (' ID ' =>11, ' area ' = ' Chaoyang ', ' pid ' =>1), array (' ID ' =>12, ' area ' = ' Haidian ', ' pid ' =>1), Array (' Id ' =>21, ' area ' = ' Nanning ', ' pid ' =>2 ', array (' ID ' =>45, ' area ' = ' Fuzhou ', ' pid ' =>4 '), array (' ID ' =>113, ' Area ' + ' Asian Games Village ', ' pid ' =>11), array (' ID ' =>115, ' area ' = ' Olympic Village ', ' pid ' =>11), array (' ID ' =>234, ' area ' = ' Wuming County ', ' pid ' =>21)), function t ($arr, $pid =0, $lev =0) {Static $list = Array (), foreach ($arr as $v) {if ($v [' pid ']== $pid) { echo str_repeat ("", $lev). $v [' area ']. " <br/> ";//output here is to see the effect $list[] = $v; t ($arr, $v [' id '], $lev + 1);}} return $list;} $list = t ($area); echo "

Summary: The above is the entire content of this article, I hope to be able to help you learn.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.