Home > Backend Development > PHP Tutorial > PHP开发(17)-callback-readdir-is_dir-foreach-glob-PhpStorm

PHP开发(17)-callback-readdir-is_dir-foreach-glob-PhpStorm

黄舟
Release: 2023-03-06 10:12:02
Original
1686 people have browsed it

PHP Development (17)-callback-readdir-is_dir-foreach-glob-PhpStorm



##

<?php
    /**
     * callback 回调函数
     */
    echo "---------- callback Demo 1 ----------<br>";
    call_user_func_array("fun",array(123,321)); // 打印结果:123 , 321 , 3
    call_user_func_array("fun",array(123,321,444,555)); // 打印结果:123 , 321 , 444

    function fun($one="1", $two="2", $three="3"){
        echo "$one , $two , $three<br>";
    }

    /**
     * 排除回文数
     * 回文数:N位数中,第1个数字和最后N个数字相同
     * strrev 反转字符串
     */
    echo "---------- callback Demo 2 ----------<br>";
    demo(25,"test"); // 打印结果:10 12 13 14 15 16 17 18 19 20 21 23 24
                    // 也就是没有打印回文数:0 1 2 3 4 5 6 7 8 9 11 22
    echo "<br>";

    function demo($num, $n){
        for ($i=0; $i<$num ; $i++){
            if (call_user_func_array($n,array($i)))
                continue;
            echo $i."    ";
        }
    }
    function test($i){
        if ($i==strrev($i))
            return true;
        else
            return false;
    }

    /**
     *
     */
    echo "---------- callback Demo 3 ----------<br>";
    //$filter = new Filter(); // 实例化Filter
    //$filter->one(1); // 调用Filter类里面的one函数
    //Filter::two(1); // 调用Filter类里面的two静态函数

    // 函数one 排除3的整数倍
    demo(25,array(new Filter(),"one")); // 打印结果:1 2 4 5 7 8 10 11 13 14 16 17 19 20 22 23
    echo "<br>";
    // 静态函数two 排除所有包含字符串3的数字
    demo(25,array("Filter","two")); // 打印结果:0 1 2 4 5 6 7 8 9 10 11 12 14 15 16 17 18 19 20 21 22 24
    echo "<br>";

    class Filter{
        function one($i)
        {
            if ($i % 3 == 0) {
                return true;
            } else {
                return false;
            }
        }
        static function two($i){
            if (preg_match(&#39;/3/&#39;,$i)){
                return true;
            }else{
                return false;
            }
        }
    }

    /**
     * 测试文件夹命令,随便找一个程序的安装目录拷贝到项目下
     * 遍历指定目录下所有目录和文件
     * readdir() 函数返回目录中下一个文件的文件名
     * is_dir() 判断给定文件名是否是一个目录
     * readdir 打印结果: .  当前目录
     * readdir 打印结果: ..  上一级目录
     */
    echo "---------- opendir ----------<br>";
    $dirname = "./demoFile2";
    // 打开目录资源
    $dir = opendir($dirname);
    // 读取文件
    echo readdir($dir).&#39;<br>&#39;;
    echo readdir($dir).&#39;<br>&#39;;
    echo readdir($dir).&#39;<br>&#39;;
    while ($file = readdir($dir)){
        $file = $dirname."/".$file;
        if (is_dir($file)){
            echo "目录:";
        }else{
            echo "文件:";
        }
        echo $file.&#39;<br>&#39;;
    }
    // 关闭目录资源
    closedir($dir);

    /**
     * 测试文件夹命令,随便找一个程序的安装目录拷贝到项目下
     * 遍历指定目录下所有目录和文件(包含子目录)
     * glob 返回匹配指定模式的文件名或目录
     * glob 返回一个包含有匹配文件 / 目录的数组
     * foreach 语法结构提供了遍历数组的简单方式
     * foreach (array_expression as $value) 每次循环中,当前单元的值被赋给 $value 并且数组内部的指针向前移一步
     * (因此下一次循环中将会得到下一个单元)
     */
    echo "---------- opendir2 ----------<br>";
    //fetchDir("demoFile1");
    function fetchDir($dir) {
        foreach(glob($dir.&#39;\*&#39;) as $file) {
            echo $file.&#39;<br>&#39;,"\n";
            if(is_dir($file)) {
                fetchDir($file);
            }
        }
    }

    fetchDir2("demoFile1");
    function fetchDir2($dir) {
        foreach(glob($dir.&#39;\*&#39;) as $file) {
            if(is_dir($file)) {
                echo "目录:".$file.&#39;<br>&#39;,"\n";
                fetchDir2($file);
            }else{
                echo "文件:".$file.&#39;<br>&#39;,"\n";
            }
        }
    }
Copy after login

The above is PHP Development (17)- The content of callback-readdir-is_dir-foreach-glob-PhpStorm, please pay attention to the PHP Chinese website (www.php.cn) for more related content!


Related labels:
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