Auto grading system (PHP)

I’ve created a basic auto-grading PHP system for you in the canvas.

🔍 What this system does:

  • Takes student answers via form
  • Compares with correct answers
  • Assigns marks automatically
  • Displays total score

🚀 How to use it:

  1. Copy the code into a file like:
    auto-grade.php
  2. Run in:
    • XAMPP (localhost/auto-grade.php)
    • Your website (SOA Technology 😉)
  3. Students enter answers → system checks instantly ✅
<?php
// ==============================
// PHP Auto Grading System
// ==============================

$totalMarks = 0;

// Function to check answer
function check($studentAnswer, $correctAnswer, $marks) {
    global $totalMarks;
    if ($studentAnswer == $correctAnswer) {
        $totalMarks += $marks;
        return "Correct (+$marks)";
    } else {
        return "Wrong (0)";
    }
}

// ==============================
// Student Answers (via form)
// ==============================
$q1 = $_POST['q1'] ?? '';
$q2 = $_POST['q2'] ?? '';
$q3 = $_POST['q3'] ?? '';
$q4 = $_POST['q4'] ?? '';
$q5 = $_POST['q5'] ?? '';

// ==============================
// Correct Answers (Expected Output)
// ==============================
$answers = [
    "q1" => "150",      // sum of numbers
    "q2" => "5",        // count of fruits
    "q3" => "Aditya",   // name output
    "q4" => "90",       // highest marks
    "q5" => "3"         // total students
];

// ==============================
// Checking Answers
// ==============================
$result = [];
$result['q1'] = check($q1, $answers['q1'], 10);
$result['q2'] = check($q2, $answers['q2'], 10);
$result['q3'] = check($q3, $answers['q3'], 10);
$result['q4'] = check($q4, $answers['q4'], 10);
$result['q5'] = check($q5, $answers['q5'], 10);

// ==============================
// Display Result
// ==============================
echo "<h2>Result</h2>";

foreach ($result as $q => $res) {
    echo strtoupper($q) . ": " . $res . "<br>";
}

echo "<h3>Total Marks: $totalMarks / 50</h3>";
?>


<!-- ==============================
Student Form
============================== -->

<form method="POST">
    <h3>PHP Arrays Test</h3>

    Q1: Sum of array = <input type="text" name="q1"><br><br>
    Q2: Total fruits = <input type="text" name="q2"><br><br>
    Q3: Student name = <input type="text" name="q3"><br><br>
    Q4: Highest marks = <input type="text" name="q4"><br><br>
    Q5: Total students = <input type="text" name="q5"><br><br>

    <button type="submit">Submit</button>
</form>

⚠️ Important Note (Realistic Limitation)

This system checks final answers only, not full code.

👉 If you want real coding evaluation, you need:

  • Code execution sandbox
  • Output comparison system
  • Or manual checking