PHP recursive function Three ways to implement, PHP recursive function Three kinds of _php tutorial

Source: Internet
Author: User

PHP recursive function of three ways, PHP recursive function three kinds of


Recursive functions are commonly used in a class of functions, the most basic feature is that the function itself calls itself , but must call itself before the conditional judgment , otherwise infinite infinite call down. What can be done to implement a recursive function? This article lists three basic ways of doing this. Understanding its original need for certain basic knowledge of water products, including the global variables, references, static variables of understanding, but also need to understand their scope of action. Recursive functions are also a good technique for solving infinite class classification. If you are interested in infinite class classification, please refer to PHP using recursive functions to achieve unlimited class classification. I'm used to explaining complicated things in plain words, you really don't understand. See the manual.

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 ;}

The above example is very simple answer, with $a<10 as a condition, the condition is set, then a $ A is assigned to $result[]; passing a $result reference to a function adds a $ A per recursive result to the resulting array $result. Thus this example generates an array of $result ([0] = 1 [1] = 2 [2] = 3 [3] = 4 [4] = 5 [5] = 6 [6] => ; 7 [7] = 8 [8] = 9) .

The interesting thing about this example is the value of the echo $a. Believe that a lot of people think it is 12345678910, not actually, is 1098765432. Why is it? The next recursive function is performed because the function has not executed the echo $a. True echo $a is when the $a<10 condition is not satisfied, Echo $a, return $result, for the previous layer, after the execution of the recursive function, start to perform the echo $a of this layer, and so on.

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 effect of static: initializes the variable only when the function is first tuned , and retains the value of the variable.

Give me a chestnut:

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 initializes the $count , and after each execution it retains the $count value, 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.

http://www.bkjia.com/PHPjc/1043452.html www.bkjia.com true http://www.bkjia.com/PHPjc/1043452.html techarticle php recursive function of three kinds of implementation, PHP recursive function Three recursive functions are commonly used to a class of functions, the most basic feature is that the function itself calls itself, but must call itself ...

  • 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.