Write PHP Script to Input Marks, Generate Result, and Display Grade (with 5 Examples)
In this tutorial, you’ll learn how to write a PHP script to input marks, generate result and display grade using various control structures. We'll also show how to save marks of English, Hindi, Marathi, Maths, and Information Technology in an array, and calculate total marks, percentage, and grades.
This is a perfect guide if you want to:
- Write a PHP script to input marks generate result and display grade
- Write PHP script to input marks generate result and display grade in Hindi
- Write a PHP program to save marks of English, Hindi, Marathi, Maths and Information Technology in an array
- Display marks of each subject along with total, average, and grade using different PHP logic approaches
Example
The Percentage = 74.2%
The Grade = 'C'
Standard Formula
Total = p + c + m + e + h
Average = total / 5.0
Percentage = (total / 500.0) * 100
Example 1: If-Else Ladder (Most Common)
<?php
$eng = 85; $hin = 78; $mar = 82; $math = 90; $it = 88;
$total = $eng + $hin + $mar + $math + $it;
$percentage = $total / 5;
echo "Subject-wise Marks:<br>";
echo "English: $eng<br>Hindi: $hin<br>Marathi: $mar<br>Maths: $math<br>IT: $it<br>";
echo "Total: $total<br>Percentage: $percentage%<br>";
if ($percentage >= 90) {
echo "Grade: A+";
} elseif ($percentage >= 75) {
echo "Grade: A";
} elseif ($percentage >= 60) {
echo "Grade: B";
} elseif ($percentage >= 50) {
echo "Grade: C";
} else {
echo "Grade: F";
}
?>
Output
Subject-wise Marks:
English: 85
Hindi: 78
Marathi: 82
Maths: 90
IT: 88
Total: 423
Percentage: 84.6%
Grade: A
Explanation
Subject-wise Marks:
English: 85
Hindi: 78
Marathi: 82
Maths: 90
Information Technology: 88
Total Marks:
The total marks are calculated by adding the marks of all five subjects:
85 + 78 + 82 + 90 + 88 = 423
Percentage:
We calculate the percentage by dividing the total marks by the maximum possible marks (500), and then multiplying by 100:
(423 / 500) * 100 = 84.6%
Grade:
The grade is determined based on the percentage using the following criteria:
-
If percentage ≥ 90, then Grade is
A+
-
If percentage ≥ 80 and < 90, then Grade is
A
-
If percentage ≥ 70 and < 80, then Grade is
B
-
If percentage ≥ 60 and < 70, then Grade is
C
-
If percentage < 60, then Grade is
F
Since the percentage is 84.6%
, the grade assigned is A
.
Example 2: Nested If Statements
<?php
// Nested if condition to assign a grade based on a single subject score
$marks = 72;
if ($marks >= 50) {
if ($marks >= 90) {
echo "Grade: A+";
} elseif ($marks >= 75) {
echo "Grade: A";
} else {
echo "Grade: B";
}
} else {
echo "Grade: F";
}
?>
Example 3: Switch Case Statement
<?php
// PHP Program to Find Grade of a Student using Switch Case
// These are the marks of five subjects
$sub_1 = 95;
$sub_2 = 85;
$sub_3 = 74;
$sub_4 = 64;
$sub_5 = 53;
// It will calculate total, average, percentage, and grade
$total = $sub_1 + $sub_2 + $sub_3 + $sub_4 + $sub_5;
$average = $total / 5.0;
$percentage = ($total / 500.0) * 100;
// It will divides average by 10 check switch expression
$sAvg = (int) ($average / 10);
// It will calculate the Grade
switch ($sAvg) {
case 10:
$grade = 'A';
break;
case 9:
$grade = 'A';
break;
case 8:
$grade = 'B';
break;
case 7:
$grade = 'C';
break;
case 6:
$grade = 'D';
break;
default:
$grade = 'E';
break;
}
// It will print the final output
echo "The Total marks = " . $total . "/500\n";
echo "The Average marks = " . $average . "\n";
echo "The Percentage = " . $percentage . "%\n";
echo "The Grade = '" . $grade . "'\n";
?>
switch (true)
is a great alternative for multiple condition checks when calculating grade.Example 4: Nested If-else to Find Percentage & Grade of a Student
<?php
// PHP Program to Find Grade of a Student using Nested If-else
// These are the marks of five subjects
$sub_1 = 85;
$sub_2 = 54;
$sub_3 = 74;
$sub_4 = 49;
$sub_5 = 38;
// It will calculate total, average, percentage, and grade
$total = $sub_1 + $sub_2 + $sub_3 + $sub_4 + $sub_5;
$average = $total / 5.0;
$percentage = ($total / 500.0) * 100;
if ($average >= 90)
$grade = "A";
else {
if ($average >= 80 && $average < 90)
$grade = "B";
else {
if ($average >= 70 && $average < 80)
$grade = "C";
else {
if ($average >= 60 && $average < 70)
$grade = "D";
else
$grade = "E";
}
}
}
// It will print the final output
echo "The Total marks = " . $total . "/500\n";
echo "The Average marks = " . $average . "\n";
echo "The Percentage = " . $percentage . "%\n";
echo "The Grade = '" . $grade . "'\n";
?>
Output
The Total marks = 300/500
The Average marks = 60
The Percentage = 60%
The Grade = 'D'
Example 5: For Loop with Associative Array
<?php
// PHP Program to find total marks & percentage using For Loop with Associative Array
$subjects = [
"English" => 85,
"Hindi" => 78,
"Marathi" => 82,
"Maths" => 90,
"Information Technology" => 88
];
$total = 0;
$subjectNames = array_keys($subjects);
for ($i = 0; $i < count($subjects); $i++) {
$key = $subjectNames[$i];
echo "$key: " . $subjects[$key] . "<br>";
$total += $subjects[$key];
}
$percentage = $total / count($subjects);
echo "Total Marks: $total<br>Percentage: $percentage%<br>";
?>
This is a complete PHP program to save marks of English, Hindi, Marathi, Maths and Information Technology in an array, iterate with a for loop, and calculate the total and percentage.
Example 6: While Loop Approach
<?php
// PHP Program to find total marks & percentage using While Loop Approach
$subjects = [
"English" => 70,
"Hindi" => 68,
"Marathi" => 74,
"Maths" => 80,
"Information Technology" => 85
];
$total = 0;
$keys = array_keys($subjects);
$i = 0;
while ($i < count($subjects)) {
echo $keys[$i] . ": " . $subjects[$keys[$i]] . "<br>";
$total += $subjects[$keys[$i]];
$i++;
}
$percentage = $total / count($subjects);
echo "Total: $total<br>Percentage: $percentage%<br>";
?>
Another variation of how to write a PHP program to input marks, generate result, and display grade using a while
loop.
Conclusion
Whether you are learning PHP or building a grading module for a school project, this guide gives you multiple approaches to:
- Write a PHP script to input marks generate result and display grade
- Use arrays, loops, conditionals, and switch-case for different use cases
- Save and display marks of individual subjects along with total marks, percentage, and grades
You May Also Like
C Program to Calculate Percentage and Grade
C++ Program to Calculate Sum of 5 Subjects and Find Percentage and Grade
Java Program to Enter Marks of Five Subjects and Calculate Total, Percentage, and Grade
Python Program to Calculate Total Marks Percentage and Grade of a Student