我是PHP初學者。我試圖用兩個文件編寫一個數字猜測腳本。在PHP中保留正確和錯誤的答案
try.php
<?php
echo "<html><head><title>Calculator Game From 1998</title></head><body>";
echo "<h1>Please Guess The Answer</h1>";
echo "<form name = \"myfirstform\" action = \"formprocess.php\" method = \"POST\">";
echo "Enter Integer Between 1-5<br>";
echo "<input type = \"text\" name = \"firstdata\">";
echo "<br> <input type= \"submit\" value = \"submit\">";
echo "</form>";
echo "</body></html>";
?>
formprocess.php
<?php
$random = rand(1, 5);
echo "<html><head><title>Answer</title></head><body>";
if ($_POST["firstdata"] == $random) {
echo "<h1><font color=\"green\">Congrulations!</h1></font><br>";
echo "<h2>Answer was: </h2>";
echo $random;
echo "<br>";
echo "<a href=\"try.php\">new game</a>";
}
else {
echo "<h1><font color=\"red\">Nope wrong answer</h1></font><br>";
echo "<h2>Answer was: </h2>";
echo $random;
echo "<br>";
echo "<a href=\"try.php\">new game</a>";
}
echo "</body></html>";
?>
的問題是,我想保持勝利,寬鬆的號碼,每個球員。我嘗試添加變量
$win = 0;
$lost = 0;
並嘗試在if部分中增加一個變量。但它不起作用,因爲在刷新頁面後它會再次爲0。
我該怎麼做?
,你需要將它存儲不知何故。您可以將其添加到數據庫或使用會話。 – andrewsi 2014-11-23 21:18:28
使用這許多'echo'屬性來輸出HTML並不是一個很好的方法。您的代碼將很快無法讀取。看到其他問題,如http://stackoverflow.com/questions/7054780/what-is-the-recommended-way-to-embed-html-codes-inside-php-codes – 2014-11-23 22:17:32