You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP测验优化:如何仅在测验结果中显示错误答案?

PHP测验结果仅显示错误答案的修改方案

我基于一段PHP测验代码制作了在线测验,当前提交后会显示所有题目(包括正确和错误的)的答案解析,现在需要修改代码,让结果页面只显示答错的题目。

原代码如下:

<?php
error_reporting(0);
$Questions = array(
    1 => array(
        'Question' => '<p>1. A set of instructions directed at a computer to solve a problem is called...</p>',
        'Answers' => array(
            'A' => 'Logic',
            'B' => 'Algorithm',
            'C' => 'Program',
            'D' => 'Design'
        ),
        'CorrectAnswer' => 'B'
    ),
    2 => array(
        'Question' => '<p>2. The science of thinking or a way of thinking with various actions that have a specific purpose according to the applicable rules...</p>',
        'Answers' => array(
            'A' => 'Algorithm',
            'B' => 'Design',
            'C' => 'Logic',
            'D' => 'Program'
        ),
        'CorrectAnswer' => 'C'
    )
);

if (isset($_POST['answers'])) {
    $Answers = $_POST['answers']; // Get submitted answers.

    foreach ($Questions as $QuestionNo => $Value) {
        // Echo the question
        echo $Value['Question'] . '<br />';

        if ($Answers[$QuestionNo] != $Value['CorrectAnswer']) {
            echo 'Your Answer: <span style="color: red;">' . $Value['Answers'][$Answers[$QuestionNo]] . '</span><br>';
            echo 'Correct Answer: <span style="color: green;">' . $Value['Answers'][$Value['CorrectAnswer']] . '</span>';
        } else {
            echo 'Your Answer: <span style="color: green;">' . $Value['Answers'][$Answers[$QuestionNo]] . '</span><br>';
            echo 'Correct Answer: <span style="color: green;">' . $Value['Answers'][$Answers[$QuestionNo]] . '</span>';
            $counter++;
        }

        echo '<br /><hr>';
        if ($counter == "") {
            $counter = '0';
            $results = "<center><p><b>You answered $counter questions</b></p></center>";
        } else {
            $results = "<center><p><b>You answered $counter questions</b></p></center>";
        }
    }
    echo $results;
} else {
?>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" id="quiz">
        <?php foreach ($Questions as $QuestionNo => $Value) { ?>

            <p><?php echo $Value['Question']; ?></p>
            <?php
            foreach ($Value['Answers'] as $Letter => $Answer) {
                $Label = 'question-' . $QuestionNo . '-answers-' . $Letter;
            ?>
                <div>
                    <input type="radio" name="answers[<?php echo $QuestionNo; ?>]" id="<?php echo $Label; ?>" value="<?php echo $Letter; ?>" />
                    <label for="<?php echo $Label; ?>"><?php echo $Letter; ?>) <?php echo $Answer; ?> </label>
                </div>
            <?php } ?>

        <?php } ?>
        <input type="submit" value="Submit Quiz" />
    </form>
<?php
}
?>

修改后的代码

<?php
error_reporting(0);
$Questions = array(
    1 => array(
        'Question' => '<p>1. A set of instructions directed at a computer to solve a problem is called...</p>',
        'Answers' => array(
            'A' => 'Logic',
            'B' => 'Algorithm',
            'C' => 'Program',
            'D' => 'Design'
        ),
        'CorrectAnswer' => 'B'
    ),
    2 => array(
        'Question' => '<p>2. The science of thinking or a way of thinking with various actions that have a specific purpose according to the applicable rules...</p>',
        'Answers' => array(
            'A' => 'Algorithm',
            'B' => 'Design',
            'C' => 'Logic',
            'D' => 'Program'
        ),
        'CorrectAnswer' => 'C'
    )
);

if (isset($_POST['answers'])) {
    $Answers = $_POST['answers'];
    $counter = 0;
    $wrongQuestions = '';

    foreach ($Questions as $QuestionNo => $Value) {
        if ($Answers[$QuestionNo] != $Value['CorrectAnswer']) {
            // 仅拼接答错的题目及答案对比
            $wrongQuestions .= $Value['Question'] . '<br />';
            $wrongQuestions .= 'Your Answer: <span style="color: red;">' . $Value['Answers'][$Answers[$QuestionNo]] . '</span><br>';
            $wrongQuestions .= 'Correct Answer: <span style="color: green;">' . $Value['Answers'][$Value['CorrectAnswer']] . '</span>';
            $wrongQuestions .= '<br /><hr>';
        } else {
            $counter++;
        }
    }

    // 统计并生成结果提示
    $totalQuestions = count($Questions);
    $wrongCount = $totalQuestions - $counter;
    $results = "<center><p><b>你答对了 $counter 道题,答错了 $wrongCount 道题</b></p></center>";
    
    // 输出答错的题目(若有)
    if ($wrongCount > 0) {
        echo '<h3>答错的题目:</h3>';
        echo $wrongQuestions;
    } else {
        echo '<center><p><b>全部答对,太棒了!</b></p></center>';
    }
    echo $results;
} else {
?>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" id="quiz">
        <?php foreach ($Questions as $QuestionNo => $Value) { ?>

            <p><?php echo $Value['Question']; ?></p>
            <?php
            foreach ($Value['Answers'] as $Letter => $Answer) {
                $Label = 'question-' . $QuestionNo . '-answers-' . $Letter;
            ?>
                <div>
                    <input type="radio" name="answers[<?php echo $QuestionNo; ?>]" id="<?php echo $Label; ?>" value="<?php echo $Letter; ?>" />
                    <label for="<?php echo $Label; ?>"><?php echo $Letter; ?>) <?php echo $Answer; ?> </label>
                </div>
            <?php } ?>

        <?php } ?>
        <input type="submit" value="Submit Quiz" />
    </form>
<?php
}
?>

修改说明

  1. 仅展示错误题目:将原循环输出所有题目的逻辑改为仅在用户答错时,才拼接对应题目和答案对比内容
  2. 优化统计逻辑:提前初始化计数器,循环结束后统一计算答对/答错数量,避免原代码中重复赋值结果的冗余操作
  3. 增强用户反馈:添加全对的专属提示,同时明确显示答对、答错的具体数量,提升结果页面的可读性

内容的提问来源于stack exchange,提问作者Harry Prasetyo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 00:35:20