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

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

黄舟
黄舟Original
2017-03-03 09:21:481561browse

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



##

";
    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
"; } /** * 排除回文数 * 回文数:N位数中,第1个数字和最后N个数字相同 * strrev 反转字符串 */ echo "---------- callback Demo 2 ----------
"; 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 "
"; 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 ----------
"; //$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 "
"; // 静态函数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 "
"; class Filter{ function one($i) { if ($i % 3 == 0) { return true; } else { return false; } } static function two($i){ if (preg_match('/3/',$i)){ return true; }else{ return false; } } } /** * 测试文件夹命令,随便找一个程序的安装目录拷贝到项目下 * 遍历指定目录下所有目录和文件 * readdir() 函数返回目录中下一个文件的文件名 * is_dir() 判断给定文件名是否是一个目录 * readdir 打印结果: . 当前目录 * readdir 打印结果: .. 上一级目录 */ echo "---------- opendir ----------
"; $dirname = "./demoFile2"; // 打开目录资源 $dir = opendir($dirname); // 读取文件 echo readdir($dir).'
'; echo readdir($dir).'
'; echo readdir($dir).'
'; while ($file = readdir($dir)){ $file = $dirname."/".$file; if (is_dir($file)){ echo "目录:"; }else{ echo "文件:"; } echo $file.'
'; } // 关闭目录资源 closedir($dir); /** * 测试文件夹命令,随便找一个程序的安装目录拷贝到项目下 * 遍历指定目录下所有目录和文件(包含子目录) * glob 返回匹配指定模式的文件名或目录 * glob 返回一个包含有匹配文件 / 目录的数组 * foreach 语法结构提供了遍历数组的简单方式 * foreach (array_expression as $value) 每次循环中,当前单元的值被赋给 $value 并且数组内部的指针向前移一步 * (因此下一次循环中将会得到下一个单元) */ echo "---------- opendir2 ----------
"; //fetchDir("demoFile1"); function fetchDir($dir) { foreach(glob($dir.'\*') as $file) { echo $file.'
',"\n"; if(is_dir($file)) { fetchDir($file); } } } fetchDir2("demoFile1"); function fetchDir2($dir) { foreach(glob($dir.'\*') as $file) { if(is_dir($file)) { echo "目录:".$file.'
',"\n"; fetchDir2($file); }else{ echo "文件:".$file.'
',"\n"; } } }

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


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