PHP-添加或减去变量

时间:2019-05-06 21:50:31

标签: php

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

1 个答案:

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