PHP测试无法正常工作

PHP测试无法正常工作,php,strcmp,Php,Strcmp,我试图做一个简单的php测试,但是我必须插入答案,并将其与strcasecmp()进行比较,这样如果第一个字母是大写或类似的话就不会有问题,但是代码工作不正常。有时,即使我插入了正确的答案,它也不会返回正确的结果。 代码如下: <?php $number = rand(1,2); $result = ""; $answer = ""; $question = ""; $question1 = "Wh

我试图做一个简单的php测试,但是我必须插入答案,并将其与strcasecmp()进行比较,这样如果第一个字母是大写或类似的话就不会有问题,但是代码工作不正常。有时,即使我插入了正确的答案,它也不会返回正确的结果。 代码如下:

<?php
         $number = rand(1,2);
         $result = "";
         $answer = "";
         $question = "";

         $question1 = "What is the capital of China";
         $question2 = "What king of country is China?";

         $answer1 = "beijing";
         $answer2 = "republic";

         if ($number == 1) {
             $answer = $answer1;
             $question = $question1;
         }

         if ($number == 2) {
             $answer = $answer2;
             $question = $question2;
         }

         if(isset($_POST['submit'])){
            if (strcasecmp($answer,$_POST['answer']) == 0) {
                 $result = "Correct!";
            } else {
                 $result = "Wrong!";
            }
         }

    ?>

    <!DOCTYPE html>
    <html>
    <head>
        <title></title>
        <link rel="stylesheet" type="text/css" href="style.css">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>

        <form method="post" action="index.php">
            <div class="middle">
                <p class="question"><?php echo $question; 
                ?></p>
                <p class="result"><?php echo $result;
                 $result = "" ?></p>
                 <div class="box">
                   <input type="text" name="answer" placeholder="Type here" class="text">
                 </div>
            </div>
            <input type="submit" name="submit" value="Continue" class="btn">
        </form>
    </body>
    </html>


通过查看您的代码,我们可以看到,当您提交表单时,您正在重新启动脚本,脚本实际上会将
$random
重置为一个新值。有两个问题,你有50%的机会得到“正确”的答案,但你会发现你的脚本在添加更多问题时根本不起作用

基本上,你应该用另一种方式来实现你想要的。您可以尝试在隐藏的
中添加问题的id,并在提交表单时进行检查,以确定是哪个问题

if(isset($_POST['submit'])){
   switch ($_POST['question']) { // Add '{'
   case 1:
       if (strcasecmp($answer1,$_POST['answer']) == 0) {
            $result = "Correct!";
       } else {
            $result = "Gresit!";
       }
       break;
   case 2:
        if (strcasecmp($answer2,$_POST['answer']) == 0) {
            $result = "Correct!";
       } else {
            $result = "Gresit!";
       } // Forgot to Add '}'
       break;
    } // Add '}' It give error in PHP 5.3 Parse error: syntax error, unexpected T_CASE, expecting ':' or '{'
}
对于HTML,您可以在表单中添加以下输入:

<input type="text" name="question" value="<?php echo $number ?>" hidden>

通过查看您的代码,我们可以看到,当您提交表单时,您正在重新启动脚本,脚本实际上会将
$random
重置为一个新值。有两个问题,你有50%的机会得到“正确”的答案,但你会发现你的脚本在添加更多问题时根本不起作用

基本上,你应该用另一种方式来实现你想要的。您可以尝试在隐藏的
中添加问题的id,并在提交表单时进行检查,以确定是哪个问题

if(isset($_POST['submit'])){
   switch ($_POST['question']) { // Add '{'
   case 1:
       if (strcasecmp($answer1,$_POST['answer']) == 0) {
            $result = "Correct!";
       } else {
            $result = "Gresit!";
       }
       break;
   case 2:
        if (strcasecmp($answer2,$_POST['answer']) == 0) {
            $result = "Correct!";
       } else {
            $result = "Gresit!";
       } // Forgot to Add '}'
       break;
    } // Add '}' It give error in PHP 5.3 Parse error: syntax error, unexpected T_CASE, expecting ':' or '{'
}
对于HTML,您可以在表单中添加以下输入:

<input type="text" name="question" value="<?php echo $number ?>" hidden>

谢谢您的编辑@Gautam D,没有看到打字扫描,请告诉我问题id的想法?我如何使用cookie或$\u会话变量?这里不是问这个的地方,因为您问了我上面回答的另一个问题。你可以使用谷歌或任何搜索引擎自己找到这个答案,因为它已经在互联网上解释了很多。您也可以将答案标记为已接受,因为这实际上是可行的。这里有一些你可以在PHP会话开始时检查的东西谢谢你的编辑@Gautam D,没有看到打字扫描你告诉我问题id的想法好吗?我如何使用cookie或$\u会话变量?这里不是问这个问题的地方,因为你问了我上面回答的另一个问题。你可以使用谷歌或任何搜索引擎自己找到这个答案,因为它已经在互联网上解释了很多。您也可以将答案标记为已接受,因为这实际上是可行的。在PHP中,您可以从会话开始检查以下内容