PHP递归统计系统中代码行数
更新时间:2019年09月19日 10:36:17 作者:轩辕朗逸
这篇文章主要为大家详细介绍了PHP递归统计系统中代码行数,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了PHP递归统计系统中代码行数的具体代码,供大家参考,具体内容如下
1、统计代码行数,必然用到的两个关键的知识点:函数递归以及文件读取。
函数递归无非就是在函数的代码中调用本身的函数名,以此形成递归循环
function A($param){
if('condition')
A($param_son);
else
return $result;
}
在文件读取中,有很多读取方式,采用了file()读取,按行读取,形成一个数组。
$file_open = file($file);
2、完整的代码
<?php
/**
* Created by PhpStorm.
* User: kung
* Date: 2015/10/16
* Time: 16:12
*/
function get_file_dir($dir){
$dir_arr = scandir($dir);
$file_arr = array();
foreach($dir_arr as $dir_one){
if(is_dir($dir.DIRECTORY_SEPARATOR.$dir_one) && $dir_one != '.' && $dir_one != '..'){
$file_arr_son = get_file_dir($dir.DIRECTORY_SEPARATOR.$dir_one);
$file_arr = array_merge($file_arr,$file_arr_son);
}else{
if($dir_one == '.' || $dir_one == '..' || strpos($dir_one,'.php') <= 0)
continue;
$file_arr[] = $dir.DIRECTORY_SEPARATOR.$dir_one;
}
}
return $file_arr;
}
$dir = dirname(__FILE__);
$file_arr = get_file_dir($dir);
$count = 0; //计算换行
$clean_count = 0;//不计算换行
foreach($file_arr as $file){
$file_open = file($file);
$line_count = count($file_open);
$count += $line_count;
foreach($file_open as $file_val){
if($file_val == PHP_EOL)/*if(nl2br($file_val) == '<br />'想通过这种方式,但是发现不可行,用var_dump(nl2br($file_val))进行打印,复制才可以*/
continue;
$clean_count++;
}
}
echo $count.'-----'.$clean_count;
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
解决PHP程序运行时:Fatal error: Maximum execution time of 30 seconds
最近做的程序中涉及到的循环比较多且处理的情况较复杂,在运行程序时出现执行超时提示如下:Fatal error: Maximum execution time of 30 seconds exceeded in D:\php\AppServ\www\sum3\test.php on line 3通过在网上搜索,找到了解决方法和大家分享,下面来一起看看吧。2016-11-11
php文件打包 下载之使用PHP自带的ZipArchive压缩文件并下载打包好的文件
php文件打包 下载之使用PHP自带的ZipArchive压缩文件并下载打包好的文件2012-06-06


最新评论