I am attempting to write to a file using PHP. I am writing data from arrays.
Here’s my code:
$subject = $_SESSION['subject'];
$section = $_SESSION['section'];
$exam1 = $_SESSION['exam1'];
$exam2 = $_SESSION['exam2'];
$finalexam = $_SESSION['finalexam'];
$average = $_SESSION['average'];
$studentArray = $_SESSION['stringArray'];
$file = $subject . "-" . $section . ".txt";
$fh = fopen($file, 'a');
$size = sizeof($studentArray);
for($i = 0; $i<$size; $i++){
fwrite($fh, $studentArray[$i] . " " . $exam1[$i] . " " . $exam2[$i] . " " . $finalexam[$i] . " " . $average[$i] . " ");
}
I always get this notice when I try to access this page:
Notice: Undefined offset: 5 in C:\xampp\htdocs\CS120\MP\Grades.php on line 26
Line 26 is the one with the fwrite in the for-loop.
There’s always a notice but the code writes to the file correctly.
Is there anything that can be done to remove/solve the notice/error?
You can use
Method 1
Method 2