Php recursive functions are implemented in three ways: php recursive functions _ PHP Tutorial

Source: Internet
Author: User
Php recursive functions are implemented in three ways: php recursive functions. Php recursive functions are implemented in three ways. php recursive functions are a common class of functions. the most basic feature is that the function itself calls itself, however, three methods of implementing php recursive functions and three methods of php recursive functions must be called.

Recursive functions are a class of functions we commonly use. the most basic feature isFunction itself calls itself, But must be before calling itselfConditional judgmentOtherwise, call the API infinitely. How can we implement recursive functions? This article lists three basic methods. Understanding it requires a certain amount of basic knowledge, including understanding global variables, references, and static variables. Recursive functions are also a good technique for solving infinite classification. If you are interested in unlimited classification, use the recursive function in php to implement unlimited classification. I use plain words to explain complex things. For more information, see the manual.

Using references as parameters

No matter whether the reference is done or not, you must first clarify what the reference is? A reference refers to two variables with different names pointing to the same storage address. Each variable has its own storage address, and values are assigned to and deleted separately. Now, two variables share a storage address. $ A = & $ B ;. In fact, $ a has to share a room with $ B regardless of its original storage address. Therefore, any change to the storage address value will affect the two values.

Functions are different, even functions with the same name. Recursive functions take reference as parameters as a bridge to form data sharing between two functions. Although the two functions seem to operate on different addresses, they actually operate on 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. when $ a <10 is used as the judgment condition and the condition is true, $ a is assigned to $ result []; the reference of $ result is passed into the function, the $ a generated by each recursion is added to the result array $ result. Therefore, the $ result Array generated in this example is Array ([0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9 ).

What is interesting in this example is the value of echo $. I believe many people think it is 12345678910. In fact, it is 1098765432. Why? Because the function has performed the next function recursion before executing echo $. When the true echo $ a is executed when the $ a <10 condition is not met, echo $ a returns $ result. for the previous layer, the recursive function is executed, start to execute echo $ a for this layer, and so on.

Use global variables

Use global variables to complete recursive functions. make sure that youUnderstandingWhat is a global variable. Global declares in the function that the variable is a reference of the same name of the external variable. VariableThe scope of the function is still in the scope of this function.. Changing the values of these variables naturally changes the values of external variables with the same name. However, once & is used, variables with the same name are no longer referenced by the same name. Using global variables to implement recursive functions does not need to understand such a deep layer. we can understand recursive functions simply by keeping the original view of global variables.

  

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

  Use static variables

  We often see static in the class. today we use it in recursive functions. Remember the role of static: onlyFirst callWhen using functionsInitializationAndReserved variable value.

Example:

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

What is the execution result of this code? Is it 00000? No. It is 01234. First, call test () and static to initialize $ count for the first time. The value of $ count will be retained after each execution and will not be initialized, it is equivalent to directly ignoring the static $ count = 0; statement.

So it is conceivable to apply static to recursive functions. Variables that need to be used as the bridge between recursive functions are initialized using static. The value of bridge variable is retained for each recursion.

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

  

  Summary

The so-called recursive function focuses on how to process the function call itself and how to ensure that the required results can be reasonably "transferred" between functions. of course, there is also a need to pass between functions that are worthy of recursion, for example:

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

In the face of such a function, we don't have to worry about it. By the way, a deep understanding of variable reference knowledge is of great benefit to solving such problems.

Three implementation methods of http://www.bkjia.com/PHPjc/1043452.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1043452.htmlTechArticlephp recursive function, php recursive function three recursive function is a class of function we commonly use, the most basic characteristic is the function itself to call itself, but must be in the 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.