php如何利用语法统计代码行数及文件数量,下面web建站小编给大家详细介绍一下具体操作流程!
1、设置php环境变量
//Windows: 我的电脑->属性->高级系统设置->PATH 加入PHP.exe的所在目录 //Linux: vim /etc/profile PATH="$PATH:/root/php/bin/"
2、新增count.php文件,输入以下代码:
<?php class TotalCode { /** * 统计当前文件有多少行代码, * @return TotalCodeInfo */ public function totalByFile($fullFileName) { $fileContent = file_get_contents($fullFileName); $lines = explode("\n",$fileContent); $lineCount = count($lines); for ($i = $lineCount -1; $i > 0; $i -= 1) { $line = $lines[$i]; if ($line != "") break; $lineCount -= 1; //最后几行是空行的要去掉。 } unset($fileContent); unset($lines); $totalCodeInfo = new TotalCodeInfo(); $totalCodeInfo->setFileCount(1); $totalCodeInfo->setLineCount($lineCount); return $totalCodeInfo; } /** * 统计当前目录下(含子目录) * 有多少文件,以及多少行代码 * * totalInfo = array( "fileCount"=>?,"lineCount"=>? ); * * @return TotalCodeInfo */ public function totalByDir($dirName) { $fileList = scandir($dirName); $totalCodeDir = new TotalCodeInfo(); foreach ($fileList as $fileName) { if ($fileName == "." || $fileName == "..") continue; $fullFileName = $dirName . "/" . $fileName; if (is_file($fullFileName)) { $totalCodeSub = $this->totalByFile($dirName . "/" . $fileName); } else if (is_dir($fullFileName)) { $totalCodeSub = $this->totalByDir($dirName . "/" . $fileName); } else { $totalCodeSub = new TotalCodeInfo(); } $totalCodeDir->increaseByOther($totalCodeSub); } return $totalCodeDir; } public function totalByDirOrFile($dirOrFileName) { if (is_dir($dirOrFileName)) { return $this->totalByDir($dirOrFileName); } else if (is_file($dirOrFileName)) { return $this->totalByFile($dirOrFileName); } else { return new TotalCodeInfo(); } } public function test() { $re = $this->totalByDir("/export/www/pm_web/configs"); var_dump($re); } public function main($dirList) { $totalCodeAll = new TotalCodeInfo(); foreach($dirList as $dirName) { $totalCodeSub = $this->totalByDirOrFile($dirName); $totalCodeAll->increaseByOther($totalCodeSub); } print_r($totalCodeAll); } } class TotalCodeInfo { private $fileCount = 0; private $lineCount = 0; public function getFileCount() { return $this->fileCount; } public function getLineCount() { return $this->lineCount; } public function setFileCount($fileCount) { $this->fileCount = $fileCount; return $this; } public function setLineCount($lineCount) { $this->lineCount = $lineCount; return $this; } /*** 累加 ***/ public function increaseByOther($totalCodeInfo) { $this->setFileCount( $this->fileCount + $totalCodeInfo->getFileCount()); $this->setLineCount( $this->lineCount + $totalCodeInfo->getLineCount()); return $this; } } $dirList = array(); $dirList[] = "./"; $obj = new TotalCode(); $obj->main($dirList); ?>
3、语法执行
php count.php
4、结果返回
TotalCodeInfo Object ( [fileCount:TotalCodeInfo:private] => 1024 [lineCount:TotalCodeInfo:private] => 551689 )
php语法对index.php首页进行判断(根据需要显示不同页面)
上面是“php如何利用语法统计代码行数及文件数量”的全面内容,想了解更多关于 php入门 内容,请继续关注web建站教程。
当前网址:https://ipkd.cn/webs_3351.html
workflows工作流
- 一条色彩斑斓的超现实小孔雀鱼ComfyUI工作流
- 一轮月亮悬挂在树上的天空中ComfyUI工作流
- 梦幻向日葵ComfyUI工作流
- 一尊白玉佛像ComfyUI工作流
- 一个破旧的工厂,一个骷髅雕像
- 嘴唇丰满的漂亮女人
- 树上一个快乐的猕猴桃ComfyUI工作流
- 一碗热气腾腾的拉面ComfyUI工作流
猜你喜欢
声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!