这是我写的代码,它不起作用。它应该询问用户一个奇数,并检查它是否正确。
代码:
$guess = $_POST['guess'];
$submit = $_POST['submit'];
if(isset($submit)){
if ($guess/2 %0){
echo "the number you are guessing is not odd";
}elseif{
($guess<$rand)
echo "Your Guess is Smaller than the secret number";
}elseif{
($guess>$rand)
echo "Your Guess is bigger than the secret number";
}else{
($guess==$rand)
echo "you guessed currectly. now try anouthe number!";}
else
header("Location: index.php");
exit();}
?>发布于 2017-12-06 09:50:56
你能试试这个吗?
如果你把'()‘放错了,你把’()‘放进了你的“()”。
<?php
$rand = rand(1, 99);
$guess = $_POST['guess'];
$submit = $_POST['submit'];
if(isset($submit))
{
if($guess / 2 % 0)
{
echo "the number you are guessing is not odd";
}
elseif($guess < $rand)
{
echo "Your Guess is Smaller than the secret number";
}
elseif($guess > $rand)
{
echo "Your Guess is bigger than the secret number";
}
elseif($guess == $rand)
{
echo "you guessed currectly. now try anouthe number!";
}
}
else
{
header("Location: index.php");
exit();
}
?>我还没有测试这段代码,所以我需要你的反馈。编辑:,你已经证实了这一点。
我想给你提供关于elseif:http://php.net/manual/en/control-structures.elseif.php的手册
请考虑更容易/更简洁的编码。就我个人而言,我喜欢使用':‘而不是'{}',当您使用HTML与PHP混合使用时,代码更少,更容易阅读,例如:
<?php
$rand = rand(1, 99);
$guess = $_POST['guess'];
$submit = $_POST['submit'];
if(isset($submit)):
if($guess / 2 % 0):
echo "the number you are guessing is not odd";
elseif($guess < $rand):
echo "Your Guess is Smaller than the secret number";
elseif($guess > $rand):
echo "Your Guess is bigger than the secret number";
elseif($guess == $rand):
echo "you guessed currectly. now try anouthe number!";
else:
header("Location: index.php");
exit();
endif;
?>不要忘记检查$_POST数据。
array也是如此,但这是一个附带注意事项:
$arr = array(1 => 'hi', 2 => 'hello'); // old
$arr = [1 => 'hi', 2 => 'hello']; // new发布于 2017-12-06 09:52:31
这不是php中的if-else构造的正确语法。
elseif部件需要在它之后有一个条件(在打开大括号之前),而else完全不需要条件。
if ($guess/2 %0){
echo "the number you are guessing is not odd";
} elseif ($guess<$rand) {
// ....
} else {
echo "you guessed currectly. now try anouthe number!";
}当然,在进行其他操作之前,您必须确保if和elseif匹配所有“错误”情况。
https://stackoverflow.com/questions/47671211
复制相似问题