繁体   English   中英

PHP-添加或减去变量

[英]PHP - Add or Subtract to variable

如何向变量添加或减去指定的数量? 我需要有一个500€的“钱包”。 现在,如果我按下按钮,则有50/50的机会我可能在500€之上获得100€ ,或者损失100€ 如果钱包为0或页面刷新,则应该结束。 (我是初学者)

试试这个简单的代码。

<?php

if($money == ''){
    $money = 500; //initial wallet amount
}

$amount = 100;// it will be added or subtracted

$wallet = $_POST['yourwallet']; //actual wallet value

if(isset($wallet)){ //check if form was submitted

    if($wallet > 0){ //you can play while the wallet value is > 0

        $p = rand(0,99); //probability

        if($p > 49){
            $money = ($wallet) + ($amount);
            echo '<center><b>';
            echo 'You won €100';
            echo '</center></b></br>';
        }else{
            $money = ($wallet) - ($amount);
            echo '<center><b>';
            echo 'You lost €100';
            echo '</center></b></br>';
        }

    }else{ //if wallet gets 0 or less you can't play anymore
            echo '<center><b>';
            echo 'Sorry, you don't have enough money to play again';
            echo '</center></b></br>';
    }
}

?>

<html>
    <center>

    <form method="post">
        Your wallet</br>
        <input type="text" name="yourwallet" readonly value="<?php echo $money ?>" /> </br>
        </br>
        <input value="Click me, you could win €100" type="submit">
    </form>

    </center>
</html>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM