duanrong0738 2014-08-30 09:43
浏览 10
已采纳

在PHP中填充有限变量的最佳解决方案是什么

Let's say I have 3 variables

$a;
$b;

$c = 30;

What I wish to do here is that I have to divide $c and put it in the first 2 variables which can be easily done by doing.

$a = $c / 2;
$b = $c / 2;

However what if in $b there is a maximum value limit of 10 and $a is limitless.
In this case the values have to be.

$a = 20;
$b = 10;

What would be the best solution for this?

  • 写回答

2条回答 默认 最新

  • duanhuoyao7011 2014-08-30 09:52
    关注

    If B has a limit, then you would need to calculate B first. Then work out the difference between C & B and set A as the answer.

    <?php
    
    $a;
    $b;
    
    $c = 100;
    
    
    $b = min(10, $c / 2);
    $a = ($c - $b);
    
    echo "A: " . $a;
    echo "B: " . $b;
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解
  • ¥15 有偿请教计算电磁学的问题涉及到空间中时域UTD和FDTD算法结合的
  • ¥15 three.js添加后处理以后模型锯齿化严重