dsbo44836129 2009-10-22 11:22
浏览 40
已采纳

PHP:将用户输入在几分钟内转换为可用于计算的值

Overall goal: User enters some number in minutes and I change those numbers from minutes to some usable form in a calculation.

How do I convert the user input in the form of minutes to some number or value that I can use later on in this php script?

<?php
$minutes = $_REQUEST["minutes"];
// We initialize the total to $1.99 for the connection fee: 
$total  = 1.99;
// For each additional minute we add $0.45?
// For example if  3 minutes are used the cost = $2, 
$addmin = 0.45;
/* How do I convert the user inputted minutes to some value
 I can manipulate with php so that I can just add via php and give the user 
 a cost, for example,  3minutes entered into a form might be = cost of $2.
 What do I need to read or study?*/
//We use a if statement here:
if (filter_has_var(INPUT_POST, "minutes"))
{
// If the user selects the number of minutes, we echo out the number of minutes.
print $minutes;
print "mins.";
// Condition pending  here: 
$total += filter_input(INPUT_POST, "$addmin");
}
print "<p>The total cost is: \$$total</p> 
";
?>
  • 写回答

2条回答 默认 最新

  • dongshanji3102 2009-10-22 11:47
    关注

    Minutes are already a usable form of number for a calculation. If you use the $minutes variable in a calculation it should automatically be cast to a number.

    // $minutes = 3, $addmin = 0.45, 3 * 0.45 = 1.35
    $subtotal = $minutes * $addmin;
    $total   += $subtotal;
    

    If you wanted the first 3 minutes to be $2 and then each additional minute to be $0.45, you'd do something like the following:

    $subtotal = 0;
    // If less than 3 minutes were input, use 3 minutes anyway.
    $minutes  = $minutes < 3 ? 3 : $minutes;  
    if ($minutes >= 3)  // If minutes
    {
        $minutes -= 3;
        $subtotal = 2;
    }
    
    // multiple the remaining minutes by the additional cost
    $subtotal += $minutes * $addmin;
    $total    += $subtotal;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c