2009-11-07 3 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주고있어 * 제발 화를 내게하지 마세요 * 그게 * 정말 * 미소 짓게 =) –

답변

1

register_globals을 종료하는 것을 잊지 마세요이 오프 아마도 (그리고 그것은 좋은 일 에코하지, 에코 사용) 따라서 $_POST['Num']$_POST['number']을 통해서만 해당 변수에 액세스 할 수 있습니다 (스크립트의 시작 부분에 $number=$_POST['number']을 할당 할 수 있음)

$number 양식을 통해 좋지 않다, 당신은 읽을 수 있습니다 php sessions

+0

$ 숫자 = $ _POST [ 'number']; $ Num = $ _POST [ 'Num']; 젠장, register_globals, 번거 로움을 피하기 위해 모든 양식에서이 작업을 시작하겠습니다. – Newb

4

나는 $ 민은 정의되지 않은 가정, 그리고 난 당신이이 형태로 정의되는 원인이 될 정의됩니다 가정합니다 가정입니다.

는 스크립트의 시작이 시도 :

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

$number이 자동으로 <input> 필드가이 값으로 설정되어 있지 않습니다. (그것은 PHP의 초기 버전에있었습니다). 이 경우 $_POST['number']$_POST['Num']을 사용해야합니다.

0

제안 :

1)

2) php.ini 파일에있는 p 태그를

관련 문제