Recursive operation mechanism and example in PHP

Source: Internet
Author: User
PHP recursion is a mechanism in which a function calls itself (directly or indirectly), and this powerful thought can make some complex concepts extremely simple. This article mainly introduces the implementation examples of recursion in PHP, not too familiar with the recursive friends in PHP can refer to this article

Definition of recursion

Recursion is a mechanism by which a function calls itself (directly or indirectly), and this powerful thought can make some complex concepts extremely simple. Outside of computer science, especially in mathematics, the concept of recursion is not uncommon. For example, the Fibonacci sequence, which is most commonly used for recursive interpretation, is a very typical example, while other classes (n!) can also be translated into recursive definitions (n! = N (n-1)! Even in real life, the idea of recursion is everywhere: for example, because of academic problems you need to seal the headmaster, but the headmaster said "only the director of Education seal I will seal", when you find the director of teaching, the director said: "Only the dean of the seal I will seal" ... Until you finally find the head teacher, in the teacher's forthright seal, you have to return to the Dean, guidance director, the final seal by the headmaster, the process is as follows:

A recursive function is a function that calls itself. Be careful when writing recursive functions, because they can be recursive indefinitely. It is important to ensure that there are adequate methods to terminate recursion.

One: Use the parameter reference to complete the recursive function. The same piece of memory address is being manipulated.

<?php$i=1; Function test (& $i) {echo $i; $i + +;  if ($i <) {test ($i);}} Test ($i);//Output 123456789test ($i);//Output 10?>


Second: Use global variables to complete the recursive function.

A real global variable imported with the global statement inside a function field actually establishes a reference to a global variable. In the example, the $i inside the test () function is actually just one application of the variable $i in the first line of the program ($i = 1;);

<?php $i = 1; function test () {global $i; echo $i; $i + +;  if ($i <10) {test ();}} Test ();//output 123456789test ();//Output 10?>

Three: Use static variables to complete the recursive function.

static function: Initializes the variable only the first time it is called, and retains the value of the variable.

<?php function Test () {   static $i = 1;   echo $i; $i + +;   if ($i <) {      test ();  }   $i--;//at the end of each level of recursion, this sentence can help to understand the recursive function of the execution process}test ();//output 123456789test ();//Output 123456789?>

Example 1. Recursively traverse all files under a folder using global variables

function GetFiles ($dir) {Global $arr, if (Is_dir ($dir)) {$hadle = @opendir ($dir), while ($file =readdir ($hadle)) {if (!in_ Array ($file, Array ('. ', '.. '))) {$dirr = $dir. '/'. $file; if (Is_dir ($DIRR)) {getFiles ($DIRR);} Else{array_push ($arr, $DIRR);}}}} $arr = Array (); GetFiles (' e:/logs ');p rint_r ($arr);

Example 2: Recursively traverse all files under a folder using a static variable

function GetFiles ($dir) {Static $arr = Array (), if (Is_dir ($dir)) {$hadle = Opendir ($dir), while ($file =readdir ($hadle)) { if (!in_array ($file, Array ('. ', '. '))) {$dirr = $dir. " /". $file; if (Is_dir ($DIRR)) {getFiles ($DIRR);} Else{array_push ($arr, $DIRR);}}} return $arr;} $rows = Array (), $rows = GetFiles (' e:/logs ');p rint_r ($rows);

Summarize

The above is a small series to introduce you to the recursive implementation of the example of PHP, I hope to help you!!

Recommended articles:

PHP Recursive algorithm simplification

Recursive functions are self-calling functions, in the body of the function directly or directly from a call from, but the requirements set from the calling condition, if satisfied with the condition, then call the function itself ...

PHP does not require recursive implementation of infinite pole classification tree

How does PHP implement an infinite pole classification tree without recursion? This paper mainly introduces the implementation of the infinite pole classification without recursion by using the pre-sequence traversal tree.

Related Article

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.