Recursive explanation in PHP

Source: Internet
Author: User
Recursion (http:/en.wikipedia.org/wiki/recursive) 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, the need for friends can refer to the following

Definition of recursion

Recursion (http:/en.wikipedia.org/wiki/recursive) is a mechanism in 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)!).

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

Related recommendations:

PHP Recursive algorithm detailed

thinkphp realization of recursive loop column and the method of infinite pole output according to tree structure

PHP implementation of First Order/middle order/post-sequence traversal binary tree operation based on non-recursive method

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.