-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask.php
More file actions
58 lines (54 loc) · 1.28 KB
/
task.php
File metadata and controls
58 lines (54 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<pre>
<?php
// Sample student records array
$students = [
[
'name' => 'John Doe',
'age' => 20,
'grade' => 'A'
],
[
'name' => 'Jane Smith',
'age' => 22,
'grade' => 'B'
],
[
'name' => 'Emily Jones',
'age' => 19,
'grade' => 'A'
],
[
'name' => 'Michael Brown',
'age' => 21,
'grade' => 'C'
]
];
// Displaying student records using a for loop
echo "Student Records:\n";
for ($i = 0; $i < count($students); $i++) {
echo "Name: " . $students[$i]['name'] . "\n";
echo "Age: " . $students[$i]['age'] . "\n";
echo "Grade: " . $students[$i]['grade'] . "\n";
echo "---------------------\n";
}
// Calculating the average age using a while loop
$totalAge = 0;
$count = 0;
while ($count < count($students)) {
$totalAge += $students[$count]['age'];
$count++;
}
$averageAge = $totalAge / count($students);
echo "Average Age: " . $averageAge . "\n";
// // Counting the number of students with grade 'A' using a do...while loop
// $gradeACount = 0;
// $i = 0;
// do {
// if ($students[$i]['grade'] == 'A') {
// $gradeACount++;
// }
// $i++;
// } while ($i < count($students));
// echo "Number of Students with Grade 'A': " . $gradeACount . "\n";
?>
</pre>