What are the advantages of using the shuffle function in PHP to randomize question order?
When creating a quiz or questionnaire in PHP, it is important to randomize the order of the questions to prevent bias or pattern recognition by the users. One way to achieve this is by using the shuffle function in PHP, which randomly shuffles the elements of an array.
// Array of questions
$questions = array("Question 1", "Question 2", "Question 3", "Question 4", "Question 5");
// Shuffle the array to randomize question order
shuffle($questions);
// Loop through shuffled array to display questions
foreach($questions as $question) {
echo $question . "<br>";
}
Keywords
Related Questions
- How can regex be used to extract all links from a webpage in PHP?
- What are the consequences of cross-posting PHP-related issues in multiple forums, and how can this practice be avoided to streamline problem-solving efforts?
- How can PHP be used to create an associative array mapping old numbers to new numbers for efficient file renaming operations?