以下是一个使用PHP编写的价格阶梯计算实例,该实例可以根据不同阶梯的价格区间计算总价。以下表格展示了不同阶梯的价格区间和对应的单价。
| 阶梯 | 价格区间(元) | 单价(元) |
|---|---|---|
| 1 | 0-100 | 10 |
| 2 | 101-500 | 8 |
| 3 | 501-1000 | 6 |
| 4 | 1001以上 | 5 |
```php

function calculatePrice($totalAmount) {
$priceStages = [
0 => ['max' => 100, 'price' => 10],
1 => ['max' => 500, 'price' => 8],
2 => ['max' => 1000, 'price' => 6],
3 => ['max' => PHP_INT_MAX, 'price' => 5]
];
$price = 0;
foreach ($priceStages as $stage) {
if ($totalAmount > $stage['max']) {
$price += ($stage['max'] - ($stage['max'] - $totalAmount) * $stage['price']);
$totalAmount = $stage['max'];
} else {
$price += $totalAmount * $stage['price'];
break;
}
}
return $price;
}
// 示例:计算1000元的总价
$totalAmount = 1000;
$totalPrice = calculatePrice($totalAmount);
echo "




