我正在编写类似于优惠券代码功能的东西,并希望能够处理设定金额代码和百分比金额。
我的代码如下;
$amount = "25"; // amount of discount
$percent = "yes"; // whether coupon is (yes) a percentage, or (no) a flat amount
if($percent == "yes"){
$newprice = ???????; // subtract $amount % of $price, from $price
}else{
$newprice = $price - $amount; // if not a percentage, subtract from price outright
}
我在您阅读本文时搜索谷歌以寻找解决方案,但我认为我也将其发布在这里以帮助可能遇到相同问题的其他人。
最佳答案
这个怎么样?
$newprice = $price * ((100-$amount) / 100);
关于php - 从 php 中的值中减去百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1864291/