Home  >  Article  >  php教程  >  Some applications of recursion (1) Traversing folders

Some applications of recursion (1) Traversing folders

WBOY
WBOYOriginal
2016-08-08 08:49:471264browse

Recursive call of function

The meaning of recursion

Recursion is actually just an algorithmic description, not a new syntax!

Sometimes, when we solve problems, we will encounter this situation. When we divide a big problem into several small problems according to a certain solution, we find that the solutions to these small problems are actually the same as the big problem just now. The solution is the same again!

Typical, such as: finding factorial!

10! = 10 * 9!

9! = 9 * 8!

8! = 8 * 7!

……

Syntactically, recursive calling of a function means that the function calls itself again during execution!

Two important points of recursion:

1, Recursive exit: refers to when to stop recursive calls

2, Recursion point: refers to when to start using recursive calls

When writing a recursive call, write the recursive exit first, and then the recursive point!

So, the characteristics of recursive calls are: the code is relatively simple to write, but when executed, consumes more memory resources !

It can also be said that The essence of recursion is to exchange space for time!

The following is a small case of traversing a folder recursively:

Code:

<?<span style="color: #000000;">php 
</span><span style="color: #008000;">/*</span><span style="color: #008000;">*
 * 定义遍历指定路径下文件与文件夹,通过递归的方法
 * @param  $dir string
 </span><span style="color: #008000;">*/</span><span style="color: #000000;">
function dirs($dir,$level</span>=<span style="color: #800080;">0</span><span style="color: #000000;">){
    </span><span style="color: #008000;">//</span><span style="color: #008000;">列出指定路径中的文件和目录</span>
    $files=<span style="color: #000000;">scandir($dir);

    </span><span style="color: #008000;">//</span><span style="color: #008000;">遍历所有的目录</span>
    <span style="color: #0000ff;">foreach</span>($files <span style="color: #0000ff;">as</span><span style="color: #000000;"> $file){

        </span><span style="color: #008000;">//</span><span style="color: #008000;">重复一个字符串</span>
        echo str_repeat(<span style="color: #800000;">'</span><span style="color: #800000;"> </span><span style="color: #800000;">'</span>,$level*<span style="color: #800080;">4</span><span style="color: #000000;">);

        </span><span style="color: #008000;">//</span><span style="color: #008000;">拼接路径</span>
        $tmpdir=$dir.<span style="color: #800000;">'</span><span style="color: #800000;">/</span><span style="color: #800000;">'</span><span style="color: #000000;">.$file;

        </span><span style="color: #008000;">//</span><span style="color: #008000;">判断是否是一个目录,文件夹</span>
        <span style="color: #0000ff;">if</span><span style="color: #000000;">(is_dir($tmpdir)){
            </span><span style="color: #008000;">//</span><span style="color: #008000;">让文件夹变成红色</span>
            echo <span style="color: #800000;">"</span><span style="color: #800000;"><font style='color:red;'>$tmpdir</font><br/><span style="color: #800000;">"</span><span style="color: #000000;">;
            </span><span style="color: #008000;">//</span><span style="color: #008000;">目录下有两个隐藏文件.和..,排除掉</span>
            <span style="color: #0000ff;">if</span>($file !=<span style="color: #800000;">'</span><span style="color: #800000;">.</span><span style="color: #800000;">'</span> && $file !=<span style="color: #800000;">'</span><span style="color: #800000;">..</span><span style="color: #800000;">'</span><span style="color: #000000;">){
                </span><span style="color: #008000;">//</span><span style="color: #008000;">通过递归的方法,调用自己,进行遍历</span>
                dirs($tmpdir,$level+<span style="color: #800080;">1</span>);<span style="color: #008000;">//</span><span style="color: #008000;">递归点</span>
<span style="color: #000000;">            }
        }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{
            </span><span style="color: #008000;">//</span><span style="color: #008000;">如果不是一个目录就直接显示这个文件</span>
            echo $file.<span style="color: #800000;">'</span><span style="color: #800000;"><br/></span><span style="color: #800000;">'</span><span style="color: #000000;">;
        }

    }
}

dirs(</span><span style="color: #800000;">'</span><span style="color: #800000;">d:/sphinx</span><span style="color: #800000;">'</span>);

Effect:

In the next article, I will use the recursive method to implement a small case of infinite classification.

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