How can PHP be utilized to track and display correct or incorrect answers in a quiz format?
To track and display correct or incorrect answers in a quiz format using PHP, you can create an array to store the correct answers and compare them with the user's input. You can then display a message indicating whether the answer was correct or incorrect.
<?php
// Array of correct answers
$correct_answers = array('A', 'B', 'C', 'D');
// User's answers
$user_answers = array('A', 'B', 'D', 'D');
// Loop through user's answers and compare with correct answers
for ($i = 0; $i < count($correct_answers); $i++) {
if ($user_answers[$i] == $correct_answers[$i]) {
echo "Question " . ($i + 1) . ": Correct!";
} else {
echo "Question " . ($i + 1) . ": Incorrect!";
}
}
?>