Home  >  Article  >  Backend Development  >  Detailed explanation of the operating mechanism and examples of recursion in PHP

Detailed explanation of the operating mechanism and examples of recursion in PHP

韦小宝
韦小宝Original
2018-02-05 10:15:391604browse

PHP recursion is a mechanism for a function to call 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 do not know much about recursion in PHP can refer to this article

Definition of recursion

Recursion 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)!) . Even in real life, recursive thinking can be seen everywhere: for example, due to academic problems, you need the principal's seal, but the principal says, "I will only stamp it if the dean of students has it." When you find the dean, the teacher The director also said: "I will stamp it only if it is stamped by the dean of the department."... Until you finally find the head teacher, and after getting the generous stamp from the head teacher, you have to return to the department head, the dean, and finally the principal. Stamp, the process is as follows:

#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
?>


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
?>

Three: Use Static variables complete recursive functions.

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 
?>

Example 1. Recursive traversal when using global variablesTraverse all files under the folder

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

Example 2: Recursive traversal under the folder when using static variables All files

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

Summary

The above is a detailed explanation of the implementation examples of recursion in PHP introduced by the editor. I hope it will be useful to everyone. Helped! !

Recommended article:

Simplified PHP recursive algorithm

The recursive function is A self-calling function calls itself directly or directly within the function body, but the conditions for self-calling need to be set. If the conditions are met, the function itself is called...

php does not require recursion Implementing Infinitus Classification Tree

phpHow to implement Infinitus Classification Tree without recursion? This article mainly introduces PHP to achieve infinite classification without recursion through pre-order traversal of the tree...

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

Statement:
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