How can PHP be optimized to handle form submissions with varying numbers of correct answers efficiently?

To optimize PHP for handling form submissions with varying numbers of correct answers efficiently, we can use an array to store the correct answers and compare them with the submitted answers. This allows for flexibility in the number of correct answers without the need to hardcode each one individually.

// Array of correct answers
$correctAnswers = ['answer1', 'answer2', 'answer3'];

// Get submitted answers from form
$submittedAnswers = $_POST['answers'];

// Check if submitted answers match correct answers
$correctCount = 0;
foreach ($submittedAnswers as $answer) {
    if (in_array($answer, $correctAnswers)) {
        $correctCount++;
    }
}

// Output the number of correct answers
echo "Number of correct answers: " . $correctCount;