如何创建一个小PHP脚本,根据部分/价格计算一些项目


How to create a little PHP script that will calculate some items based on portion/price?

我想用PHP写一个脚本,它会帮我做一些数学运算。

它应该完成以下任务:

示例

假设你有一家果汁网店。你想

  1. 将几种果汁保存到数据库中(想想:橙汁、柠檬汁、苹果汁)。

  2. 您还想节省这些果汁的份量和价格(例如100毫升/2.50美元)。

  3. 你想混合/混合不同的果汁,得到毫升/美元的总量。

如果我们将其相加:

橙汁:100毫升/2.50美元|我要50毫升=1.25美元。

苹果汁:100毫升/1.50美元|我要10毫升=0.15美元。

混合果汁总量:60毫升=1.40美元

到目前为止还不错。我写了以下代码:

<?php
  function devide() {
  $portion = 100;         //variable for ml    
  $price = 2.50;          //variable for price
  $portionNew = 50;       //value for new portion put into total mix
    if($portionNew > 0) {
      echo ($price / 100 * $portionNew);  //calculates price of new portion
    } 
  }
      echo "Total Juice" . (" = ");  
      echo devide() . (" USD") ;
?>

这个代码只计算你从一杯果汁中取出的那部分的价格。如果你从100毫升果汁中取出30毫升,它将与价格相呼应。

这些是我的问题:

  1. 我如何使这个脚本不仅反映新的价格,而且反映新的部分($portionNew)?

  2. 关于我希望最终脚本做什么,函数是正确的开始吗?或者我应该考虑对象或数组吗?

  3. 我应该如何继续这个脚本,这样它就会添加多个($portionNews/ $price)来告诉我最终的价格和我的总果汁混合物的比例?

  1. 只需在devide函数中添加类似echo 'Portion: '.$portionNew的内容。(我相信你可能想称之为divide?)

  2. 您可以创建一个Juice类,但我认为在这种情况下这太过分了。我可能会坚持使用一个数组来存储果汁及其价格,以及一个混合多种果汁的功能。

  3. 你可以创建一个混合果汁的功能。您可以指定一个变量,该变量包含当前价格和混合物中的部分、要添加的果汁的类型和数量,它将返回一个表示混合物中果汁的新价格和数量的值。

如果你想把果汁储存在一个阵列中,它可能看起来像这样:

// In this case, we have an array that maps the juice type to a price (in cents
// per 100 ml).
$juices = array(
    'orange' => 250,
    'apple' => 150
);
// If you decide you need to store more information about a juice, you can change
// this array to map a juice type to another associative array that contains the
// price and your additional information.
// Beware: if you use this, you have to change the mixJuices function to access
// a juice's "price" value:
//     $pricePerLiter = $juices[$juiceType]['price'] * 10;
$juices = array(
    'orange' => array(
        'price' => 250,
        'color' => 'orange'
    ),
    'apple' => array(
        'price' => 150,
        'color' => 'yellow'
    )
);

请注意,我将价格设置为美分,而不是美元。通过这样做,我避免了在计算价格时使用浮点和所有与之相关的问题(请参阅PHP文档中关于浮点精度的红色通知)

混合果汁的功能可能是这样的:

// The function needs to know what's currently in the mix, which is why it's
// taking the $currentMix as a parameter. Also, it needs to know about all the
// available juices, which is why it's taking the $juices as another parameter.
// A mix is represented by an associative array that contains the current amount
// of juice in the mix (identified by "portion") and the current price
// (identified by "price"). Note that you need to pass the $juices array I
// defined earlier, so it can access the different types of juices and their 
// prices.
function mixJuices($currentMix, $juices, $juiceType, $juicePortion)
{
    $pricePerLiter = $juices[$juiceType] * 10;
    $price = $pricePerLiter * $juicePortion / 1000;
    $currentMix['portion'] = $currentMix['portion'] + $juicePortion;
    $currentMix['price'] = $currentMix['price'] + $price;
    return $currentMix;
}

该函数也可以编写为使用全局$mix变量。如果您这样做了,就不需要将$currentMix传递给函数。然而,您也将失去同时创建多个混合的能力(因为对修改后的mixJuices函数的所有调用都将修改相同的全局混合)。

你可以像这样使用我上面定义的函数:

// At the beginning, there's no juice at all, so it doesn't cost anything (yet)
$mix = array('portion' => 0, 'price' => 0);
// Let's mix some juice in
$mix = mixJuices($mix, $juices, 'apple', 500);
$mix = mixJuices($mix, $juices, 'orange', 250);
// Print the amount of juice and the price
// The price is divided by 100 so we can display it in dollars instead of cents
echo sprintf('%d milliliters of this juice mix will cost %.2f USD.',
    $mix['portion'], $mix['price'] / 100);

这种方法可以让你做各种有趣的事情。

假设你有两个顾客想要点果汁混合物。通过创建两个不同的$mix阵列,您可以轻松混合同一类型的多种果汁:

// Process customer 1's order
$mixCustomer1 = array('portion' => 0, 'price' => 0);
$mixCustomer1 = mixJuices($mixCustomer1, $juices, 'apple', 250);
$mixCustomer1 = mixJuices($mixCustomer1, $juices, 'orange', 250);
echo sprintf('%d milliliters of this juice mix will cost %.2f USD.',
    $mixCustomer1['portion'], $mixCustomer1['price'] / 100);
// 500 milliliters of this juice mix will cost 10.00 USD.
// Process customer 2's order
$mixCustomer2 = array('portion' => 0, 'price' => 0);
$mixCustomer2 = mixJuices($mixCustomer2, $juices, 'apple', 150);
$mixCustomer2 = mixJuices($mixCustomer2, $juices, 'orange', 350);
echo sprintf('%d milliliters of this juice mix will cost %.2f USD.',
    $mixCustomer2['portion'], $mixCustomer2['price'] / 100);
// 500 milliliters of this juice mix will cost 11.00 USD.

现在假设客户2有某种代金券,任何果汁都可以打八折:

// Apply the discount
$discountedJuices = array_map(function($price) { return (1 - 0.2) * $price; }, $juices);
// Process customer 2's order
$mixCustomer2 = array('portion' => 0, 'price' => 0);
$mixCustomer2 = mixJuices($mixCustomer2, $discountedJuices, 'apple', 150);
$mixCustomer2 = mixJuices($mixCustomer2, $discountedJuices, 'orange', 350);
echo sprintf('%d milliliters of this juice mix will cost %.2f USD.',
    $mixCustomer2['portion'], $mixCustomer2['price'] / 100);
// 500 milliliters of this juice mix will cost 8.80 USD.