donglulong0877 2018-05-19 16:35
浏览 14
已采纳

使用Session时PHP条件失败

I made a card game with PHP but there I'm facing some issue.

if ($_SESSION["bet"] != NULL)
{
    echo "Your starting bankroll is: " . $_SESSION["bankroll"] . "<br>";
    echo "Your bet is: " . $_SESSION["bet"] . "<br>";
}

I'm getting an input from the user. The problem is, when the game loads at first, and the user enters an input and clicks submit, the game won't work. The condition ($_SESSION["bet"] != NULL) is giving true and bankroll is not defined.

Is there a way I can set this up properly? Is there some PHP method that can initialize the variable once then only works it on session start, then the rest of the code can take care of how that variable gets updated? The bankroll variable gets initialized if the user clicks submit without anything in it right now, so the game still works but it starts improperly.

if ($_SESSION["bet"] == NULL)
{  
  $_SESSION["bankroll"] = 1000;
}

The bankroll variable gets initialized to 1000 every time user submits a NULL input. I want to change this.

More code... Updating...

session_start();

$_SESSION["bet"] = $_POST["bet"];

echo "<br>";

//print_r($_SESSION);

if ($_SESSION["bet"] != NULL)

{

echo "Your starting bankroll is: " . $_SESSION["bankroll"] . "<br>";

echo "Your bet is: " . $_SESSION["bet"] . "<br>";

}


if ($_SESSION["bet"] == NULL)

{  

  $_SESSION["bankroll"] = 1000;

}


else if ($_SESSION["bet"] > 1000 || $_SESSION["bet"] < 0)

{  

   echo " Please enter between 0 and 1000.";

}


else if ($_SESSION["bet"] > $_SESSION["bankroll"])

{

  echo "You can't enter more than what you have.";

}

else

{

$deck = array();

for($x = 0; $x < 54; $x++) {

    $deck[$x] = $x;

}

shuffle($deck);

//Then more stuff.  This one for example...

    if(($houseSuits[0] == -100) || ($houseSuits[1] == -100) || ($houseSuits[2] == -100) || ($houseSuits[3] == -100))
         {
            echo "<br>";
            echo '<center> PLAYER WINS! (HOUSE HAS JOKER) </center>';
            echo "<br>";
            $_SESSION["bankroll"] = $_SESSION["bankroll"] + $_SESSION["bet"]; //THESE NEED TO BE ADDRESSED.
         }

I JUST WANT TO FIND A WAY TO INITIALIZE BANKROLL TO 1000 AT START. THE WAY I'M DOING IT IS BY SUBMITTING A NULL VALUE THEN ASSUMING USER NEVER SUBMITS NULL VALUE AGAIN.

I WOULD LIKE BANKROLL TO BE INITIALIZED TO 1000, THEN FOR THE GAME TO TAKE CARE OF HOW BANKROLL GETS UPDATED.

I FOUND A WAY TO DO IT BUT IT'S NOT A PROPER WAY SO THAT'S WHY I'M ASKING FOR HELP.

THANK YOU.

  • 写回答

1条回答 默认 最新

  • dse323222 2018-05-19 17:00
    关注

    Ok try this.

    So if the bankroll is not set, then set it, once it's set it wont get set again because it's set.

    Then any conditions after are fine.

    session_start();
    
    // Initialise bankroll if not already
    if (!isset($_SESSION['bankroll'])) {
        $_SESSION['bankroll'] = 1000;
        echo "Your starting bankroll is: " . $_SESSION["bankroll"] . "<br>";
    }
    
    $_SESSION['bet'] = $_POST['bet'];
    
    if ($_SESSION['bet'] != NULL) {
        echo "Your bet is: " . $_SESSION['bet'] . "<br>";
    }
    
    
    if ($_SESSION['bet'] > 1000 || $_SESSION['bet'] < 0) {
    
        echo " Please enter between 0 and 1000.";
    
    } elseif ($_SESSION['bet'] > $_SESSION['bankroll']) {
    
        echo "You can't enter more than what you have.";
    
    } else {
        // Your card game stuff
    }
    

    Notes: you may have issues with using session as it'll store their bet wherever they are, so issuing a bet sets the session, then navigate elsewhere and come back and their bet will still be as before. Maybe this doesn't matter.
    You also might not want to check if the bet is null, more perhaps empty or whatever. Test either way to be sure.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作