PHP çok yüksek veya çok düşük yineleyemezsiniz

4 Cevap

[Yasal Uyarı:. PHP için yeni ve ben sadece teşekkür ederim, bir öğrenmeye çalışıyor bu yüzden, gerçekten öğrenme sürecini engelleyen hiçbir flamers lütfen, öğreniyorum]

Çalışır Aşağıdaki kodu, tek sorun sayısı çok yüksek veya çok düşük, ben yanlış bir şey yapıyorum zaman kullanıcı anlatmak değil, ama ben hata göremiyorum ki?

<?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>

4 Cevap

register_globals php.ini muhtemelen Off (ve bu iyi bir şey) ve bu nedenle yalnızca $_POST['Num'] yoluyla bu değişkenleri erişebilir ve $_POST['number'] (sadece can senaryonun uzaklaştırmak $number=$_POST['number'] atamak)

Ayrıca, $number form aracılığıyla gizli gönderme güzel değil, okumak isteyebilirsiniz yaklaşık php sessions

Ben $ Num tanımsız varsayarak yaşıyorum ve ben bunu şeklinde tanımlanır neden olması tanımlanacak varsayarak varsayarak yaşıyorum.

Senaryonun başlangıcında bu deneyin:

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

$number otomatik olarak <input> alanında değere ayarlı değil. (Bu PHP erken sürümlerinde idi). Şimdi bunun için $_POST['number'] ve $_POST['Num'] kullanmak zorunda.

Öneriler:

1) echo kullanmak, Echo değil

2) p etiketi kapatmak unutmayın