我试图比较从一个选择器(HTML)获取的值和已经存在于数组中的值,但出现了问题。
当我比较$a和$b时,即使它们相等(例如答案是A,正确答案也是A),我总是得到“你失败了”的结果。我尝试将它们都转换为字符串并使用双等号和三等号进行比较,但结果总是错误的。
编辑:以下是整段代码:
这是我的测试网站:https://students.btsi.lu/oliwi/Jose/login.php。
if(isset($_POST['submit1'])) {
$selectOption = $_POST['answers'];
$correctAnswer = $filearray[$question][6];
$a=(string)$selectOption;
$b=(string)$correctAnswer;
echo $a;
echo $b;
if ( $a=== $b){
echo 'Nice answer';
}else{
echo 'you failed';
}
}
当我比较$a和$b时,即使它们相等(例如答案是A,正确答案也是A),我总是得到“你失败了”的结果。我尝试将它们都转换为字符串并使用双等号和三等号进行比较,但结果总是错误的。
编辑:以下是整段代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Beat the L</title>
</head>
<body>
<h1>Beat the L</h1>
<!--<div id="indexFormDiv">
<form method="post">
<label for="Username">Username: </label><input id="Username" name="Username" type="text" value="" required><br>
<button name="submit" id="submit" >Let's go</button>
</form>
</div> -->
<?php
$question = 0;
$dataText = file('test.txt');
$arrayAnswers = array(
2=> 'A',
3=> 'B',
4=> 'C',
5=> 'D'
);
$filearray = array();
$file = fopen("test.txt", "r");
if ($file) {
$i = 0;
while (($line = fgets($file)) !== false) {
$data = explode(":", $line);
$filearray[$i] = array(
1 => $data[0],
2 => $data[1],
3 => $data[2],
4 => $data[3],
5 => $data[4],
6 => $data[5]
);
$i++;
}
fclose($file);
} else {
echo "Couldn't load the questions";
}
echo "Question: " . $filearray[$question][1] . "<br>";
echo "<form method='post' action='". $_SERVER['PHP_SELF']."'>";
echo "Choose your answer: <select name='answers'>";
for ($i = 2; $i <= count($filearray[$question])-1; $i++) {
echo "<option value='".$arrayAnswers[$i]."'>" . $filearray[$question][$i] . "</option>";
}
echo "</select><br>";
echo "<input type='submit' name='submit1' id='submit1' ></form>";
if(isset($_POST['submit1'])) {
$selectOption = $_POST['answers'];
$correctAnswer = $filearray[$question][6];
$a=(string)$selectOption;
$b=(string)$correctAnswer;
echo $a;
echo $b;
if ( $a=== $b){
echo 'Nice answer';
} else {
echo 'you failed';
}
}
?>
</body>
</html>
这是我的测试网站:https://students.btsi.lu/oliwi/Jose/login.php。
$arrayAnswers
。$filearray[$question][6]
只是选项的文本,而不是值。 - Laurenstrim
注意D
后面的空格... - dWinder