我是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