如何将输入与数组php进行比较

如何将输入与数组php进行比较,php,Php,我正在做一个反义词测验。这个想法是在一个词的提示下产生的。你必须用相反的回答。我制作了两个数组,其中包含单词列表和一组预定义的示例问题。我的想法是,我将把这个词与一个相反的词进行比较,它会一直持续下去,直到找到正确的输入词为止 由于某种原因,它不起作用。我对PHP相当陌生,我相信有一种更简单的方法可以做到这一点。现在,这是我必须处理的问题 我还想用数组来实现这一点,而不是用MySQL <?php $wl1 = array('Hot', 'Summer', 'Hard', '

我正在做一个反义词测验。这个想法是在一个词的提示下产生的。你必须用相反的回答。我制作了两个数组,其中包含单词列表和一组预定义的示例问题。我的想法是,我将把这个词与一个相反的词进行比较,它会一直持续下去,直到找到正确的输入词为止

由于某种原因,它不起作用。我对PHP相当陌生,我相信有一种更简单的方法可以做到这一点。现在,这是我必须处理的问题

我还想用数组来实现这一点,而不是用MySQL

    <?php
    $wl1 = array('Hot', 'Summer', 'Hard', 'Dry', 'Simple', 'Light', 'Weak', 'Male', 'Sad', 'Win', 'Small', 'Ignore', 'Buy', 'Succeed', 'Reject', 'Prevent',
    'Exclude');
    $wl2 = array('Cold', 'Winter', 'Soft', 'Wet', 'Complex', 'Darkness', 'Strong', 'Female', 'Happy', 'Lose', 'Big', 'Pay Attention', 'Sell', 'Fail', 'Accept',
    'Allow', 'Include');

    $compl = array("Hot is to cold",
                   "Summer is to winter",
                   "Hard is to soft",
                   "Dry is to wet",
                   "Simple is to complex",
                   "Light is to darkness",
                   "Weak is to strong",
                   "Male is to female",
                   "Sad is to happy",
                   "Win is to lose",
                   "Small is to big",
                   "Ignore is to pay attention",
                   "Buy is to sell",
                   "Succeed is to fail",
                   "Reject is to accept",
                   "Prevent is to allow",
                   "Exclude is to include");

    $complr = $compl[array_rand($compl)];
    $wl2r = $wl2[array_rand($wl1)];
    $q = $complr . " as ".$wl2r." is to "."<br>";
    echo $q;

    if(isset($_POST['submit'])){
        $score = 0;
        $answer = $_POST['answer'];
            if($wl2r == "Cold" && $answer == "Hot"){
                echo "Correct";
                $score++;
            }
            if($wl2r == "Winter" && $answer == "Summer"){
                echo "Correct";
                $score++;
            }
            if($wl2r == "Soft" && $answer == "Hard"){
                echo "Correct";
                $score++;
            }
            if($wl2r == "Wet" && $answer == "Dry"){
                echo "Correct";
                $score++;
            }
            if($wl2r == "Complex" && $answer == "Simple"){
                echo "Correct";
                $score++;
            }
            if($wl2r == "Darkness" && $answer == "Light"){
                echo "Correct";
                $score++;
            }
            if($wl2r == "Strong" && $answer == "Weak"){
                echo "Correct";
                $score++;
            }
            if($wl2r == "Female" && $answer == "Male"){
                echo "Correct";
                $score++;
            }
            if($wl2r == "Happy" && $answer == "Sad"){
                echo "Correct";
                $score++;
            }
            if($wl2r == "Lose" && $answer == "Win"){
                echo "Correct";
                $score++;
            }
            if($wl2r == "Big" && $answer == "Small"){
                echo "Correct";
                $score++;
            }
            if($wl2r == "Pay Attention" && $answer == "Ignore"){
                echo "Correct";
                $score++;
            }
            if($wl2r == "Sell" && $answer == "Buy"){
                echo "Correct";
                $score++;
            }
            if($wl2r == "Fail" && $answer == "Succeed"){
                echo "Correct";
                $score++;
            }
            if($wl2r == "Accept" && $answer == "Reject"){
                echo "Correct";
                $score++;
            }
            if($wl2r == "Allow" && $answer == "Prevent"){
                echo "Correct";
                $score++;
            }
            if($wl2r == "Include" && $answer == "Exclude"){
                echo "Correct";
                $score++;
            }
        echo $score;


    }







?>


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Opposites</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>
<body>
    <div class="form-group">
        <form action="task3.php" method="post">
            Enter your Answer <input type="text" name="answer">
            <input type="submit" name='submit'>
        </form>
    </div>
</body>
</html>

对立面
输入您的答案

一个问题是,如果没有数据库,就不可能在页面加载之间持久保存程序所需的状态数据。例如,在提交后,无法知道在上一次页面加载时进行了哪些随机选择,或者分数是多少。实现这一点的最佳方法是使用数据库或Ajax,但如果没有数据库或Ajax,则可以(但不是非常优雅地)使用隐藏的输入字段传递数据,而无需花费太多精力。您还可以探索本地存储和JavaScript,这不会太麻烦

关于游戏逻辑,考虑使用关联数组。数组键不是数字序列0、1、2…,而是使用第一对的字符串作为键来检索其伙伴。这消除了巨大的条件块、大量键入和可能的错误:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Opposites</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>
<body>

<?php
$compliments = [
    'Hot' => 'Cold',
    'Summer' => 'Winter',
    'Hard' => 'Soft',
    'Dry' => 'Wet',
    'Simple' => 'Complex',
    'Light' => 'Dark',
    'Weak' => 'Strong',
    'Male' => 'Female',
    'Sad' => 'Happy',
    'Win' => 'Lose',
    'Small' => 'Big',
    'Ignore' => 'Pay Attention',
    'Buy' => 'Sell',
    'Succeed' => 'Fail',
    'Reject' => 'Accept',
    'Prevent' => 'Allow',
    'Exclude' => 'Include'
];
$score = isset($_POST['score']) ? (int)$_POST['score'] : 0;

if (isset($_POST['answer']) && isset($_POST['question'])) {
    if ($_POST['answer'] === $compliments[$_POST['question']]) {
        echo "<div>Correct, " . $_POST['question'] . " is to " . $_POST['answer'] . ".</div>";
        $score++;
    }
    else {
        echo "<div>Incorrect. " . $_POST['question'] . " is to " . 
             $compliments[$_POST['question']] . ", not to " . $_POST['answer'] . ".</div>";
    }
}

$sample = array_rand($compliments);
$test = array_rand($compliments);
echo "<div>Score: $score</div>";
echo "<div>$compliments[$sample] is to $sample as $test is to </div>";
?>

    <div class="form-group">
        <form action="task3.php" method="post">
            Enter your Answer <input type="text" name="answer">
            <input type="hidden" name="question" value="<?= isset($test) ? $test : "" ?>"></input>
            <input type="hidden" name="score" value="<?= isset($score) ? $score: "" ?>"></input>
            <input type="submit" name='submit'>
        </form>
    </div>
</body>
</html>

对立面
输入您的答案

我试过了,结果总是不正确。它如何知道它是否正确?您是否通过在测试值中硬编码来测试它<代码>$test='Ignore'$回答=‘注意’将计算为true。通过在数组中键入来确定正确的值<例如,code>$compaids['Hot']==='Cold'
是正确的,而
$compaids['Small']==='Pay'
不是(把这些文字字符串想象成你的变量)。是的,硬代码可以工作,只是不明白为什么它不能自己工作。我不确定你的意思是什么——你需要展示一个具体的例子。因为我在测试套件中硬编码了一个随机答案,所以你只有17分之一的机会看到
“Correct”
。在您的web应用程序中试用,如果仍然不起作用,请告诉我,或者使用
readline
在CLI上输入。注意区分大小写,在两个字符串上使用
strtolower
忽略大小写。我认为我们彼此误解了。例如,当出现“Include”一词时,如果插入“Exclude”一词,它会说是正确的;如果插入“Sad”一词,它会说是“不正确的”。我不希望它有一个正确的机会。