search
Homephp教程php手册Some applications of recursion (1) Traversing folders

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!