[免责声明:我是PHP的新手,我只是在学习,所以请不要浪费,当人们试图学习时,它确实会阻碍学习过程,谢谢。]
下面的代码运行,唯一的问题是它没有告诉用户什么时候数字太高或太低,我做错了什么,但我看不出错误?
<?php
//Starts our php document
if (!$number)
//if we have already defined number and started the game, this does not run
{
Echo"Please Choose a Number 1-100 <p>";
//gives the user instructions
$number = rand (1,100) ;
//creates number
}
else {
//this runs if the game is already in progress
if ($Num >$number)
{
Echo "Your number, $Num, is too high. Please try again<p>";
}
//if the number they guessed is bigger than number, lets user know, guess was high
elseif ($Num == $number)
{
Echo "Congratulations you have won!<p>";
//if the number they guessed was correct it lets them know they won
Echo "To play again, please Choose a Number 1-100 <p>";
$number = rand (1,100) ;
//it then starts the game again by choosing a new value for $number that they can guess
}
else
{
Echo "Your number, $Num, is too low. Please try again<p>";
}
//if the answer is neither correct or to high, it tells them it is too low
}
?>
<form action = "<?php Echo $_SERVER[’PHP_SELF’]; ?>" method = "post"> <p>
<!--this sends the form back to the same page we are on-->
Your Guess:<input name="Num" />
<input type = "submit" name = "Guess"/> <p>
<!--Allows the user to input their guess-->
<input type = "hidden" name = "number" value=<?php Echo $number ?>>
<!--keeps passing along the number value to keep it consistent till it is guessed-->
</form>
</body>
</html>
答案 0 :(得分:4)
我假设$ Num未定义,我假设您假设它将被定义为因为它在表单中定义。
在脚本开头尝试:
if(!empty($_POST)) {
$Num = (int) $_POST['Num'];
}
答案 1 :(得分:2)
$number
不会自动设置为<input>
字段的值。 (它是在PHP的早期版本中)。您现在必须使用$_POST['number']
和$_POST['Num']
。
答案 2 :(得分:1)
register_globals
可能是Off(这是一件好事),因此你只能通过$_POST['Num']
和$_POST['number']
访问这些变量(你可以只分配{{} 1}}在你的剧本开始时)
另外,通过表单发送秘密$number=$_POST['number']
并不好,您可能想了解php sessions
答案 3 :(得分:0)
建议:
1)使用echo,而不是Echo
2)不要忘记关闭p标签