嘿,我很接近fininshing我的猜測一個數字遊戲,非常簡單的PHP,但由於某種原因,我卡住了。我以隱藏的形式存儲變量,但顯然每次發送頁面時都會重置該數字,因此您永遠無法獲得正確的數據。猜數字遊戲的問題
任何想法?我的代碼如下。
<?php
// generate a random number for user to guess
$number = rand(1,100);
if($_POST["guess"]){
// grab the user input guess
$guess = $_POST['guess'];
$numbe = $_POST['number'];
if ($guess < $number){
echo "Guess Higher";
}elseif($guess > $number){
echo "Guess Lower";
}elseif($guess == $number){
echo "You got it!";
}
echo "<br />Random Number:".$number."<br />";
echo $guess;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Guess A Number</title>
</head>
<body>
<form action="<?=$_SERVER['PHP_SELF'] ?>" method="post" name="guess-a-number">
<label for="guess">Guess A Number:</label><br/ >
<input type="text" name="guess" />
<input name="number" type="hidden" value="<?= $number ?>" />
<input name="submit" type="submit" />
</form>
</body>
</html>
你有一個錯字$美元 – SilentGhost 2009-02-13 14:43:21
是的,錯字。 作爲額外的練習,您應該將您的代碼轉換爲使用method =「GET」,但仍然將答案隱藏在URL中。 – kmkaplan 2009-02-13 14:46:25