dsh8009271 2019-05-06 21:50
浏览 60

PHP - 添加或减去变量

How can I add or subtract a specified amount to a variable? I need to have a "wallet" that indicates 500€. Now if I press a button there is a 50/50 chance that I either get 100€ on top of the 500€ or lose 100€. It should end if either the wallet gets to 0 or the page gets refreshed. (I'm a beginner)

  • 写回答

1条回答 默认 最新

  • douyaju4749 2019-05-06 23:09
    关注

    try this simple code.

    <?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>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错