PHP统计单选按钮值:20道AB型题目混合计分需求
Got it, here's a clean, maintainable solution to calculate your quiz score based on specific A/B option combinations. I'll split this into two parts: the HTML form (for users to answer) and the PHP logic (to compute the score).
Step 1: The HTML Quiz Form
First, let's set up the form with your radio buttons. I'll include a few examples, and you can extend it to all 20 questions:
<!-- quiz-form.php --> <form method="POST" action="calculate-score.php"> <div> 问题1: <input type="radio" name="1" value="A" required> A <input type="radio" name="1" value="B" required> B </div> <div> 问题2: <input type="radio" name="2" value="A" required> A <input type="radio" name="2" value="B" required> B </div> <div> 问题3: <input type="radio" name="3" value="A" required> A <input type="radio" name="3" value="B" required> B </div> <!-- Add your remaining 17 questions here, following the same pattern --> <button type="submit">提交计算得分</button> </form>
Note: The required attribute ensures users can't skip questions—remove it if you want to allow unanswered questions.
Step 2: PHP Scoring Logic
Create a separate file (calculate-score.php) to handle the form submission and score calculation. The key here is using an array to define your scoring rules, making it super easy to adjust later:
<?php // calculate-score.php if ($_SERVER['REQUEST_METHOD'] !== 'POST') { // Redirect users back to the form if they access this page directly header('Location: quiz-form.php'); exit; } // Define your scoring rules: [question number, target option, points to add] // Adjust this array to match your exact scoring needs (e.g., $1A, $2B, $4A, etc.) $scoringRules = [ ['question' => 1, 'option' => 'A', 'points' => 1], ['question' => 2, 'option' => 'B', 'points' => 1], ['question' => 4, 'option' => 'A', 'points' => 1], ['question' => 7, 'option' => 'B', 'points' => 1], // Add all 20 of your question rules here ]; $totalScore = 0; foreach ($scoringRules as $rule) { $questionKey = (string)$rule['question']; // Check if the question was answered (in case you removed the "required" attribute) if (isset($_POST[$questionKey])) { $userAnswer = $_POST[$questionKey]; // Sanitize input to only accept valid A/B values if (in_array($userAnswer, ['A', 'B'], true)) { // Add points if the user's answer matches the rule's target option if ($userAnswer === $rule['option']) { $totalScore += $rule['points']; } } } } // Display the result to the user echo "<h2>您的最终得分: $totalScore</h2>"; ?>
Key Features of This Implementation
- Maintainable: All scoring rules are in one array—no need to rewrite logic if you change which questions contribute to the score.
- Secure: Input sanitization ensures only valid A/B values are processed, preventing unexpected data from breaking the code.
- Flexible: You can easily adjust points per question (e.g., make some options worth 2 points instead of 1) by changing the
pointsvalue in the rules array. - Error Resilient: Handles unanswered questions gracefully if you choose not to use the
requiredattribute.
Optional Enhancements
- Validation: Add server-side validation to ensure all questions are answered (even if you used the
requiredattribute, it's good to double-check). - Transparency: Show users which questions contributed to their score by adding a section that lists each rule and whether they matched it.
- Persistence: Save scores to a database if you need to track user results over time.
内容的提问来源于stack exchange,提问作者Ruben

