Cognitive Function Test
<?php // Define the test questions and correct answers $questions = array( “What is 2 + 2?” => 4, “Which planet is closest to the Sun?” => “Mercury”, “What is the capital of France?” => “Paris”, “Who wrote ‘Romeo and Juliet’?” => “William Shakespeare”, ); // Initialize variables for scoring $totalQuestions = count($questions); $correctAnswers = 0; // Process submitted answers if ($_SERVER[‘REQUEST_METHOD’] === ‘POST’) { foreach ($questions as $question => $correctAnswer) { $userAnswer = isset($_POST[$question]) ? $_POST[$question] : ”; if (strtolower($userAnswer) == strtolower($correctAnswer)) { $correctAnswers++; } } // Calculate the user’s score $score = ($correctAnswers / $totalQuestions) * 100; // Determine the cognitive function level if ($score >= 90) { $cognitiveLevel = “High School or above”; } elseif ($score >= 70) { $cognitiveLevel = “Middle School”; } else { $cognitiveLevel = “Elementary School or below”; } } ?> <!DOCTYPE html> <html> <head> <title>Cognitive Function Test</title> </head> <body> <h1>Cognitive Function Test</h1> <?php if ($_SERVER[‘REQUEST_METHOD’] === ‘POST’): ?> <p>Your Score: <?php echo $score; ?>%</p> <p>Your Cognitive Function Level: <?php echo $cognitiveLevel; ?></p> <?php else: ?> <form method=”POST”> <?php foreach ($questions as $question => $correctAnswer): ?> <label for=”<?php echo $question; ?>”><?php echo $question; ?></label> <input type=”text” name=”<?php echo $question; ?>” required><br> <?php endforeach; ?> <input type=”submit” value=”Submit”> </form> <?php endif; ?> </body> </html>