PHP帮助编号生成器

时间:2013-05-28 10:45:44

标签: php

这是我的代码:

<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放置了一些研究我不知道如何使用它们,因为我说Iam是编程的新手!

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

2 个答案:

答案 0 :(得分:1)

您有以下代码:

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

 } else {
     $count = 1;
 }

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

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

最后,$count + 1应为++ $count

答案 1 :(得分: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。