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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀