-1

这是我的代码:

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

2 回答 2

1

你有这个代码:

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

 } else {
     $count = 1;
 }

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

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

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

于 2013-05-28T10:50:17.793 回答
0
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。

于 2013-05-28T10:50:48.110 回答