PHP错误:未定义的偏移量:2

I'm having an issues with this PHP code. Its supposed to extract the total amount of errors that occur in a log file. Every time I run it, I get this error: Undefined offset: 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;
}
?>

Any help would be great

You need to check if the array offset exists before attempting to access it:

 $statusCode = substr($parts[2], 0, 4);

should be

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