PHP recursive usage and recursive directory examples_PHP tutorial

WBOY
Release: 2016-07-13 10:48:04
Original
1171 people have browsed it

In PHP, the recursive algorithm is one of the few data traversal methods we have. Let me introduce to you some useful things using recursion.

Look at a simple recursion example

Example 1

if($a > 1){
The code is as follows
 代码如下 复制代码

function demo($a) { 

    static $sum=1; 

    if($a > 1){ 

        $sum*=$a; 

        demo(--$a); 

    }else{ 

        $a=$sum; 

        } 

        return $sum; 


 

echo demo(10);

Copy code

function demo($a) {

 代码如下 复制代码

class listdir{
var $depth;
var $dirname;
var $list;
var $tostring;

function listdir($dir){
$this->dirname=$dir;
$this->depth=0;
$this->tostring=”";
}

//把结果保存进多维数组
function getlist($dir=”"){
if($dir==”")$dir=$this->dirname;
$d=@dir($dir);
while(false!==($item=$d->read()))
{
if($item!=”.”&&$item!=”..”)
{
$path=$dir.”/”.$item;
if(is_dir($path)){
$this->depth+=1;
$this->getlist($path);
}else{
$this->list[$this->depth][]=$item;
}
}
}
$this->list[$this->depth]['directory']=$dir;
$this->depth-=1;
$d->close();
return $this->list;
}

//字符窜化结果

function tostring($dir=”"){
if($dir==”")$dir=$this->dirname;
$d=@dir($dir);
$this->tostring.=”

    n”;
    $this->tostring.=”Directory:”.$dir.”n”;
    while(false!==($item=$d->read()))
    {
    if($item!=”.”&&$item!=”..”)
    {
    $path=$dir.”/”.$item;
    if(is_dir($path)){
    $this->depth+=1;
    $this->tostring($path);
    }else{
    $this->tostring.=”
  • ”.$item.”
  • n”;
    }
    }
    }
    $this->depth-=1;
    $d->close();
    $this->tostring.=”
n”;
return $this->tostring;
}
}
$wapdir=”jquery”;
$d=new listdir($wapdir);
echo $d->tostring();
?>

输出结果:


    Directory:jquery
  • jquery-1.3.2.js

  • jquery-1.3.2.min.js

  • jquery-1.3.2-vsdoc2.js

  • test.html

  • common.js


    • Directory:jquery/d
    • common.js

    • jquery-1.3.2.js


static $sum=1;
$sum*=$a;

demo(--$a); }else{ $a=$sum; }   return $sum; }
echo demo(10);
Example 2 Traverse directories
The code is as follows Copy code
class listdir{
var $depth;
var $dirname;
var $list;
var $tostring;<🎜> <🎜>function listdir($dir){
$this->dirname=$dir;
$this->depth=0;
$this->tostring=””;
} //Save the result into a multi-dimensional array
function getlist($dir=""){
if($dir==”")$dir=$this->dirname;
$d=@dir($dir);
while(false!==($item=$d->read()))
{
if($item!=”.”&&$item!=”..”)
{
$path=$dir.”/”.$item;
if(is_dir($path)){
$this->depth+=1;
$this->getlist($path);
}else{
$this->list[$this->depth][]=$item;
}
}
}
$this->list[$this->depth]['directory']=$dir;
$this->depth-=1;
$d->close();
return $this->list;
} //Character channeling result function tostring($dir=""){
if($dir==”")$dir=$this->dirname;
$d=@dir($dir);
$this->tostring.=”
    n”;
    $this->tostring.=”Directory:”.$dir.”n”;
    while(false!==($item=$d->read()))
    {
    if($item!=”.”&&$item!=”..”)
    {
    $path=$dir.”/”.$item;
    if(is_dir($path)){
    $this->depth+=1;
    $this->tostring($path);
    }else{
    $this->tostring.=”
  • ”.$item.”
  • n”;
    }
    }
    }
    $this->depth-=1;
    $d->close();
    $this->tostring.=”
n”;
return $this->tostring;
}
}
$wapdir=”jquery”;
$d=new listdir($wapdir);
echo $d->tostring();
?> Output result:

    Directory:jquery
  • jquery-1.3.2.js

  • jquery-1.3.2.min.js

  • jquery-1.3.2-vsdoc2.js

  • test.html

  • common.js


    • Directory:jquery/d
    • common.js

    • jquery-1.3.2.js


http://www.bkjia.com/PHPjc/632807.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632807.htmlTechArticleIn php, the recursive algorithm is a data traversal method that we can’t compare with. Let me tell you about it below. Let’s introduce some useful things using recursion. Look at a simple recursion example...
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!