2009-11-07 40 views
1

[免责声明:我是PHP新手,我只是在学习,所以请别激动,这真的会阻碍学习过程,当你试图学习时,谢谢。 ]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

我给+1的先发制人*请不要火焰我*这*真的*让我微笑=) – 2009-11-07 21:49:28

回答

1

register_globals在php.ini可能是关(这是一件好事),因此您只能通过$_POST['Num']$_POST['number']访问这些变量(你可以在你的脚本beggining)

也分配$number=$_POST['number'],发送的秘密$number通过形式是不好的,你可能想要阅读关于php sessions

+0

$ number = $ _POST [ '号码']; $ Num = $ _POST ['Num']; 该死的register_globals,我将开始在我的所有表格上做这件事以避免麻烦,谢谢。 – Newb 2009-11-08 08:19:34

4

我假设$ Num是未定义的,我假设您假设它将被定义为因为它在窗体中定义。

在脚本的开始试试这个:

if(!empty($_POST)) { 
    $Num = (int) $_POST['Num']; 
} 
2

$number不会自动设置为<input>字段的值。 (这是在早期版本的PHP中)。您现在必须为此使用$_POST['number']$_POST['Num']

0

建议:

1)使用的回声,而不是echo

2)不要忘记关闭p标签