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

PHP测验脚本答完最后一题报错:未定义数组键2,求解决

PHP测验脚本完成最后一题后报错的解决方法

问题现象

答完最后一道题目后脚本不会停止运行,反而抛出以下错误:

Warning: Undefined array key 2 in /customers/5/8/b/kbekker.dk/httpd.www/Fag/Dansk/Opgaver/Ordklasser/find_navneord_001.php on line 71
Warning: Trying to access array offset on value of type null in /customers/5/8/b/kbekker.dk/httpd.www/Fag/Dansk/Opgaver/Ordklasser/find_navneord_001.php on line 71 3

错误指向第71行代码:

<label for="question"><?php echo ($currentQuestion+1).". ". $questionsAndAnwsers[$currentQuestion]["question"];?></label>

问题代码

<?php
// Declare all your questions and answers as multi dimensional array
$questionsAndAnwsers = array(array("question" => "1. How many sides does a triangle have?", "answer" => "three"),
                            array("question" => "2. How many sides does a square have?", "answer" => "four"));
      
// current question
$currentQuestion = 0;
if(isset($_POST["currentQuestion"])){
    $currentQuestion = $_POST["currentQuestion"];
    if($_POST["guess"] == $questionsAndAnwsers[$currentQuestion]["answer"]){
        // answer was correct, increment your question-counter for the next question
        $currentQuestion++;
        // print success message
        echo "Det var rigtigt";
    } else {
        // question was wrong, counter stays as it is, so same question will be printed
        // print failure message
        echo "prøv igen";
    }
}
?>
<form method="POST" action="">
    <label for="question"><?php echo ($currentQuestion+1).". ". $questionsAndAnwsers[$currentQuestion]["question"];?></label>
    <input type="hidden" name="currentQuestion" value="<?php echo $currentQuestion;?>">
    <input type="text" name="guess" value="">
    <input type="submit">
</form>

错误原因与修复方案

错误本质是:当答对最后一题后,$currentQuestion会被递增到2,但题目数组只有索引0和1两个元素,访问不存在的索引2就会触发报错。需要在代码中加入「完成状态判断」,当所有题目答完后,停止渲染表单并显示完成提示。

修复后的代码:

<?php
// 声明题目与答案的多维数组
$questionsAndAnwsers = array(
    array("question" => "How many sides does a triangle have?", "answer" => "three"),
    array("question" => "How many sides does a square have?", "answer" => "four")
);

// 当前题目索引
$currentQuestion = 0;
$totalQuestions = count($questionsAndAnwsers); // 获取总题数

if(isset($_POST["currentQuestion"])){
    $currentQuestion = $_POST["currentQuestion"];
    // 先判断当前题目索引是否有效,避免非法提交
    if($currentQuestion < $totalQuestions){
        if($_POST["guess"] == $questionsAndAnwsers[$currentQuestion]["answer"]){
            $currentQuestion++;
            echo "Det var rigtigt";
        } else {
            echo "prøv igen";
        }
    }
}

// 检查是否完成所有题目
if($currentQuestion >= $totalQuestions){
    echo "<h2>测验完成!你答对了所有题目!</h2>";
} else {
?>
<form method="POST" action="">
    <label for="question"><?php echo ($currentQuestion+1).". ". $questionsAndAnwsers[$currentQuestion]["question"];?></label>
    <input type="hidden" name="currentQuestion" value="<?php echo $currentQuestion;?>">
    <input type="text" name="guess" value="" required>
    <input type="submit" value="提交答案">
</form>
<?php
}
?>

关键修改点

  1. 新增$totalQuestions = count($questionsAndAnwsers)获取总题数,用于完成状态判断
  2. 处理POST请求时,先校验当前题目索引的有效性,防止非法提交引发错误
  3. 增加完成判断逻辑:当$currentQuestion >= $totalQuestions时,显示完成提示,不再输出表单
  4. 移除题目文本中自带的序号,避免和代码生成的序号重复
  5. 给输入框添加required属性,强制用户输入答案(可选优化)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 10:05:19