国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

php遞歸用法與遞歸目錄實(shí)例

2024-05-04 21:58:06
字體:
供稿:網(wǎng)友

在php中遞歸算法是我們比得不多的一種數(shù)據(jù)遍歷方式了,下面我來給大家介紹一下利用遞歸來做一下用的東西吧,看一個(gè)簡單的遞歸實(shí)例.

例1,代碼如下:

  1. function demo($a) {  
  2.  
  3.     static $sum=1;  
  4.  
  5.     if($a > 1){  
  6.  
  7.         $sum*=$a;  
  8.  
  9.         demo(--$a);  
  10.  
  11.     }else{  
  12.  
  13.         $a=$sum;  
  14.  
  15.         }  
  16.  
  17.         return $sum;  
  18.  
  19. echo demo(10); 

例2,遍歷目錄,代碼如下:

  1. <?php 
  2. class listdir{ 
  3. var $depth
  4. var $dirname
  5. var $list
  6. var $tostring
  7.  
  8. function listdir($dir){ 
  9. $this->dirname=$dir
  10. $this->depth=0; 
  11. $this->tostring=”"; 
  12.  
  13. //把結(jié)果保存進(jìn)多維數(shù)組 
  14. function getlist($dir=”"){ 
  15. if($dir==”")$dir=$this->dirname; 
  16. $d=@dir($dir); 
  17. while(false!==($item=$d->read())) 
  18. if($item!=”.”&&$item!=”..”) 
  19. $path=$dir.”/”.$item
  20. if(is_dir($path)){ 
  21. $this->depth+=1; 
  22. $this->getlist($path); 
  23. }else
  24. $this->list[$this->depth][]=$item
  25. $this->list[$this->depth]['directory']=$dir
  26. $this->depth-=1; 
  27. $d->close(); 
  28. return $this->list; 
  29.  
  30. //字符竄化結(jié)果 
  31.  
  32. function tostring($dir=”"){ 
  33. if($dir==”")$dir=$this->dirname; 
  34. $d=@dir($dir); 
  35. $this->tostring.=”<UL>n”; 
  36. $this->tostring.=”Directory:”.$dir.”n”; 
  37. while(false!==($item=$d->read())) 
  38. if($item!=”.”&&$item!=”..”) 
  39. $path=$dir.”/”.$item
  40. if(is_dir($path)){ 
  41. $this->depth+=1; 
  42. $this->tostring($path); 
  43. }else
  44. $this->tostring.=”<LI>”.$item.”</LI>n”; 
  45. $this->depth-=1; 
  46. $d->close(); 
  47. $this->tostring.=”</UL>n”; 
  48. return $this->tostring; 
  49. $wapdir=”jquery”; 
  50. $d=new listdir($wapdir); 
  51. echo $d->tostring(); 
  52. ?> 
  53. /* 
  54. 輸出結(jié)果: 
  55. <UL> 
  56. Directory:jquery 
  57. <LI>jquery-1.3.2.js</LI> 
  58. <LI>jquery-1.3.2.min.js</LI> 
  59. <LI>jquery-1.3.2-vsdoc2.js</LI> 
  60. <LI>test.html</LI> 
  61. <LI>common.js</LI> 
  62. <UL> 
  63. Directory:jquery/d 
  64. <LI>common.js</LI> 
  65. <LI>jquery-1.3.2.js</LI> 
  66. </UL> 
  67. </UL> 
  68. */ 

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 宣化县| 芦山县| 洞头县| 鲁山县| 报价| 海安县| 兴安县| 莆田市| 太康县| 类乌齐县| 多伦县| 万全县| 寿阳县| 三门峡市| 禹州市| 黄冈市| 庆城县| 陆良县| 那坡县| 肇源县| 雅安市| 乌恰县| 延津县| 大埔区| 安丘市| 呈贡县| 孟连| 公主岭市| 平泉县| 化德县| 专栏| 凤凰县| 东宁县| 天祝| 昌邑市| 太原市| 中牟县| 佛冈县| 濮阳市| 鄂托克前旗| 安平县|