以下是一个简单的PHP源码报表实例,它可以帮助你生成一个关于项目代码的统计报告。这个报告将包括文件数量、代码行数、注释行数、空行数等信息。

```php

实例php源码报表,PHP源码报表实例:如何生成项目代码统计报告  第1张

// 定义源码目录

$sourceDir = 'path/to/your/source/directory';

// 初始化统计数据

$totalFiles = 0;

$totalLines = 0;

$totalComments = 0;

$totalEmptyLines = 0;

// 遍历目录下的所有文件

function scanDirectory($dir) {

global $totalFiles, $totalLines, $totalComments, $totalEmptyLines;

if (!is_dir($dir)) {

return;

}

$files = scandir($dir);

foreach ($files as $file) {

if ($file == '.' || $file == '..') {

continue;

}

$filePath = $dir . DIRECTORY_SEPARATOR . $file;

if (is_dir($filePath)) {

scanDirectory($filePath);

} else {

analyzeFile($filePath);

}

}

}

// 分析单个文件

function analyzeFile($filePath) {

global $totalFiles, $totalLines, $totalComments, $totalEmptyLines;

$fileHandle = fopen($filePath, 'r');

if (!$fileHandle) {

return;

}

$lines = [];

$comments = 0;

$emptyLines = 0;

while (($line = fgets($fileHandle)) !== false) {

$line = trim($line);

if (empty($line)) {

$emptyLines++;

} elseif (preg_match('/^""s*""/""*.*""*""/""s*$/', $line)) {

$comments++;

} else {

$totalLines++;

$lines[] = $line;

}

}

if (feof($fileHandle)) {

fclose($fileHandle);

}

$totalFiles++;

$totalComments += $comments;

$totalEmptyLines += $emptyLines;

}

// 开始扫描目录

scanDirectory($sourceDir);

// 输出统计结果

echo "