我遇到了这个PHP代码的问题。它应该提取日志文件中发生的错误总数。每次运行它,我都会收到此错误:未定义的偏移量:2
<?php
$handle = fopen('../../apache2/logs/error.log','r') or die ('File opening failed');
$requestsCount = 0;
$numerror = 0;
while (!feof($handle)) {
$dd = fgets($handle);
$requestsCount++;
$parts = explode('"', $dd);
$statusCode = substr($parts[2], 0, 4);
if (hasRequestType($statusCode, 'error')) $numerror++;
}
echo "Total Errors: " . $numerror . "<br />";
fclose($handle);
function hasRequestType($l,$s) {
return substr_count($l,$s) > 0;
}
?>
任何帮助都会很棒
答案 0 :(得分:0)
在尝试访问数组偏移之前,您需要检查数组偏移是否存在:
$statusCode = substr($parts[2], 0, 4);
应该是
if (array_key_exists(2, $parts)) {
$statusCode = substr($parts[2], 0, 4);
} else $statusCode = -1; //or whatever you want to do if there is no status code