How can PHP be used to create a quiz with randomized questions and answers?
To create a quiz with randomized questions and answers in PHP, you can store the questions and answers in arrays, shuffle the arrays to randomize the order, and then loop through the shuffled arrays to display the quiz to the user.
<?php
// Define an array of questions and answers
$questions = array(
"What is the capital of France?",
"Who wrote 'Romeo and Juliet'?",
"What is the tallest mountain in the world?",
);
$answers = array(
"Paris",
"William Shakespeare",
"Mount Everest",
);
// Shuffle the questions and answers arrays
shuffle($questions);
shuffle($answers);
// Display the randomized quiz
for ($i = 0; $i < count($questions); $i++) {
echo "Question " . ($i + 1) . ": " . $questions[$i] . "<br>";
echo "Answer: " . $answers[$i] . "<br><br>";
}
?>
Keywords
Related Questions
- How can one prevent SQL injections in PHP scripts, as mentioned in the forum thread?
- Is it recommended to use phpMyAdmin for exporting and importing larger databases, or are there alternative methods like using mysqldump and mysql?
- Are there any recommended resources or tutorials for learning advanced string manipulation techniques in PHP?