PHP关于数字生成器的帮助


PHP help on number Generater

这是我的代码:

<html>
    <title>Game Process</title>
    </head>
    <body>
    <?php
    $userguess= $_POST["number"];
    $gennumber = rand(0,20);
    if ($gennumber > $userguess) {
        $result="lost";
        echo "You have lost your guess was too low! The number was:".$gennumber;
    } elseif ($gennumber == $userguess){
        echo "Wow you are extreamly lucky to get ".$gennumber;  $result="won";
    } else {
        echo "You have lost your guess was too high! The number was: ".$gennumber;
    $result="lost";
    }
    ?>

这就是我试图观察$count是否等于零以及是否将其设置为1的问题。我对issetempty做了一些研究,但我不知道如何使用它们,正如我所说,我是编程新手!

    <?php
    $_SESSION['count']=$count;
    if (!$count){
         } else {
             $count = 1;
         }

    if ($result == "lost"){
    echo '<a href="Game.php"><input type="button" value="Have another guess!"/></a>';
    $count +1;
    echo "<br>your number of guess are: ".$count; 
    } else {
        echo "well done!<br>";
        echo "your number of guess are: ".$count; 
    }
    ?>
    </body>
    </html>

您有以下代码:

$_SESSION['count']=$count;
if (!$count){
 } else {
     $count = 1;
 }

您正在将$count分配给$_SESSION数组中的一个值。您不会在那里更改$count的值。如果你想检查$count是否等于0,为什么不能使用:

if($count == 0)
{
    $count = 1;
}

最后,$count + 1应该是++ $count

if (isset($_SESSION['count']) && !empty($_SESSION['count']) && is_numeric($_SESSION['count'])) {
    $count = (int)$_SESSION['count'];
} else {
    $count = 0;
    $_SESSION['count'] = (int)$count;
}
if ($result == "lost"){
    echo '<a href="Game.php"><input type="button" value="Have another guess!"/></a>';
    $count++; //increase $count var
    $_SESSION['count'] = (int)$count;
    echo "<br>your number of guess are: ".$count; 
} else {
    echo "well done!<br>";
    echo "your number of guess are: ".$count; 
}

检查这是否为您处理:(我正在检查会话中接收的值是否已设置,而不是空的,以及它是否为数字。如果是这样,我将把它保存到变量$count中,并将其强制转换为整数(只是为了确定(。

否则,将其设为0。