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.

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度