我想检查两个变量是否相同,并根据指定的条件回显消息。我不得不在某处出错。如果两个变量都是相同的岩石/岩石,纸张/纸张等,这只是抽签的一个测试条件......我输入它以查看它是否可以工作。
PHP代码
<?php
$items = Array('rock','paper','scissors');
$randomChoice = $items[array_rand($items)];
$choice = $_POST['choice'];
if ($choice == $randomChoice) {
echo "Its a draw!";
} else {
header('Location: index.php');
}
HTML CODE
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<h3>Rock! Paper! Scissors</h3>
<form action="handle.php" method="POST">
<p>Chose one of the options</p>
<select name="choice" required>
<option value="Rock">Rock</option>
<option value="Paper">Paper</option>
<option value="Scissors">Scissors</option>
</select>
<input type="submit" name="submit" value="Submit my choice">
</form>
</body>
</html>
答案 0 :(得分:0)
它不起作用,因为您的选项值以大写字母开头,但数组中的项目以小写字母开头。
所以你决定要改变什么...我宁愿把所有东西都用小写:
ORDER BY col_field1, col_field2
答案 1 :(得分:0)
您应该验证用户提供的数据。
$items = array('rock','paper','scissors');
$choice = array_key_exists('choice', $_POST) ? strtolower($_POST['choice']) : null;
if (!in_array($choice, $items, true)) {
die('Show an error message or do something here');
}
以上将确保在$_POST
中提供选择值并强制使用小写值,否则将$choice
变量设置为null
。
然后验证提供的值是$items
数组中的值之一。
如果您想要不区分大小写的比较,可以使用strcasecmp
strcasecmp($choice, $randomChoice) === 0
$items = array('rock','paper','scissors');
$randomChoice = $items[0];
$choice = 'Rock';
if (strcasecmp($choice, $randomChoice) === 0) {
echo "Its a draw!";
} else {
echo $randomChoice;
}
结果:
"Its a draw!"
或者,您可以更改选择值以匹配php大小写。
参考文献: