Home > Backend Development > PHP Tutorial > Detailed explanation of recursion in PHP

Detailed explanation of recursion in PHP

墨辰丷
Release: 2023-03-26 09:16:02
Original
3178 people have browsed it

Recursion (http:/en.wikipedia.org/wiki/Recursive) is a mechanism in which a function calls itself (directly or indirectly). This powerful idea can make some complex concepts extremely simple. . This article mainly introduces the detailed implementation examples of recursion in PHP. Friends who need it can refer to

Definition of recursion

Recursion (http :/en.wikipedia.org/wiki/Recursive) is a mechanism in which a function calls itself (directly or indirectly). This powerful idea can make some complex concepts extremely simple. Outside of computer science, especially in mathematics, the concept of recursion is common. For example: the Fibonacci sequence, which is most commonly used to explain recursion, is a very typical example, and others such as hierarchy (n!) can also be transformed into recursive definitions (n! = n*(n-1)!) .

A recursive function is a function that calls itself. Be careful when writing recursive functions, as they may recurse indefinitely. You must ensure that there are adequate means to terminate recursion.

1: Use parameter reference to complete the recursive function. The operation is the same memory address.

<?php
$i=1; 
function test(&$i) 
{
echo $i; 
$i++; 
 if ($i < 10) 
{ 
test($i);
} 
} 
test($i);// 输出123456789
test ( $i );// 输出10
?>
Copy after login

Two: Use global variables to complete the recursive function.

A real global variable imported with the global statement inside the function domain actually establishes a reference to the global variable. In the example, $i inside the test() function is actually just an 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();// 输出123456789
test ();// 输出10
?>
Copy after login

Three: Use static variables to complete the recursive function.

The role of static: initialize the variable only when the function is called for the first time, and retain the variable value.

<?php 
function test () 
{ 
  static $i = 1 ; 
  echo $i ;
$i ++; 
  if ( $i < 10 ) { 
     test ();
  } 
  $i --;// 在每一层递归结束时自减,这一句可以帮助理解递归函数的执行过程 
}
test();// 输出123456789
test();// 输出123456789 
?>
Copy after login

Example 1. Recursively traverse all files in 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(&#39;.&#39;, &#39;..&#39;)) )
{
$dirr = $dir.&#39;/&#39;.$file;
if(is_dir($dirr))
{
getFiles($dirr);
}else{
array_push($arr, $dirr);
}
}
}
}
}
$arr = array();
getFiles(&#39;E:/logs&#39;);
print_r($arr);
Copy after login

Example 2: Recursively traverse all files in a folder using static variables

function getFiles ($dir)
{
static $arr = array();
if(is_dir($dir)){
$hadle = opendir($dir);
while($file=readdir($hadle))
{
if(!in_array($file,array(&#39;.&#39;,&#39;..&#39;)) )
{
$dirr = $dir."/".$file;
if(is_dir($dirr))
{
getFiles ($dirr);
}else{
array_push($arr,$dirr);
}
}
}
}
return $arr;
}
$rows= array();
$rows = getFiles (&#39;E:/logs&#39;);
print_r($rows);
Copy after login

Related recommendations:

Detailed explanation of PHP recursive algorithm

thinkPHP implements a method of implementing recursive loop columns and outputting infinitely according to the tree structure

PHP implements pre-order/in-order/post-order traversal binary tree operations based on non-recursive algorithms

The above is the detailed content of Detailed explanation of recursion in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template