Three methods for implementing php recursive functions and how to implement the number accumulate _ php instance

Source: Internet
Author: User
What are the methods for implementing recursive functions? How can we use recursive functions to accumulate numbers? This article mainly introduces three methods for implementing php recursive functions and how to accumulate numbers. For more information, see. Recursive functions are commonly used in programming. They are characterized by the fact that they can call themselves, but they must be judged by conditions before being called. Otherwise, they can be called infinitely. This article lists three methods to implement recursive functions. The first method is to use references as parameters, the second method is to use global variables, and the third method is to use static variables to understand such problems, for reference and static variables, you also need to understand the scope of their functions. This is not a nonsense. For more information, see the following.

Method 1: Use reference as a parameter

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;. Actually, it refers$Regardless of the original storage address$ BShare the room. 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.

The Code is as follows:


Function test ($ a = 0, & $ result = array ()){
$ A ++;
If ($ a <10 ){
$ Result [] = $;
Test ($ a, $ result );
}
Echo $;
Return $ result;
}

The above example is very simple,A <10As the judgment condition, if the condition is true, a is assigned to result []. The reference of result is passed into the function, and a generated by every recursion is added to the result array result. Therefore,$ ResultArray isArray ([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:Echo. I believe many believe that12345678910Actually, it is not.1098765432. Why? Because the function has performed the next function recursion before executing echo.

Real executionEchoYeswhenA <10When the conditions are not met,Echo, ReturnResult,For the previous layer, after executing the recursive function, start to executeEcho $,And so on.

Method 2: Use global variables

Make sure you understand what global variables are.. GlobalDeclare that the variable in the function is a reference of the same name of the external variable. The variable is still within the scope of this function. Changing the values of these variables naturally changes the values of external variables with the same name. However&The variable with the same name is 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.

The Code is as follows:


Function test ($ a = 0, $ result = array ()){
Global $ result;
$ A ++;
If ($ a <10 ){
$ Result [] = $;
Test ($ a, $ result );
}
Return $ result;
}

Method 3: use static variables

We often seeStaticToday, we use it in recursive functions. Remember the role of static: Initialize the variable only when the function is called for the first time, and keep the variable value.

  Example:

The Code is as follows:


Function test (){
Static $ count = 0;
Echo $ count;
$ Count ++;
}
Test ();
Test ();
Test ();
Test ();
Test ();

  

What is the execution result of this code? Yes00000? No. Yes01234. First callTest (), staticPair$ CountAfter initialization, each execution is retained.$ CountIs not initialized, which is equivalent to directly ignoringStatic $ count = 0.

So it is conceivable to apply static to recursive functions. Use variables that need to be used as a bridge between recursive functionsStaticFor initialization, every recursion will be retained"Bridge variable".

The Code is as follows:


Function test ($ a = 0 ){
Static $ result = array ();
$ A ++;
If ($ a <10 ){
$ Result [] = $;
Test ($ );
}
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:

The Code is as follows:


Function test ($ a = 0 ){
$ A ++;
If ($ a <10 ){
Echo $;

Test ($ );
}
}

The following code demonstrates how php uses recursive functions to accumulate numbers.

The Code is as follows:

The Code is as follows:


<? Php
Function summation ($ count ){
If ($ count! = 0 ):
Return $ count + summation ($ count-1 );
Endif;
}
$ Sum = summation (10 );
Print "Summation = $ sum ";
?>

It is not necessary to worry about php recursive functions. a deep understanding of variable reference knowledge is helpful for solving such problems, the above content is the three methods for implementing php recursive functions and how to accumulate numbers. I hope this will help you in your future studies.

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.