Php 如果数量为6或6的倍数,则可享受1%的折扣

Php 如果数量为6或6的倍数,则可享受1%的折扣,php,ajax,Php,Ajax,嗨,朋友们>我想知道如果数量是6或12或18或24,那么每种产品的价格都会打9折 例如,如果数量为8,那么6个数量的价格将打10%的折扣,其余2个数量将不打折。 总数量=8 6数量*10%产品价格 2数量*完整产品价格 8个数量的总价=(6个数量价格加10%折扣)+(2个数量加全价) 我想在PHP这个。请帮帮我我想这是你能做到的一种方法 $quantity = 8; $remainder = $quantity % 6 ? $quantity % 6 : 0; $discountItems =

嗨,朋友们>我想知道如果数量是6或12或18或24,那么每种产品的价格都会打9折

例如,如果数量为8,那么6个数量的价格将打10%的折扣,其余2个数量将不打折。 总数量=8 6数量*10%产品价格 2数量*完整产品价格 8个数量的总价=(6个数量价格加10%折扣)+(2个数量加全价)


我想在PHP这个。请帮帮我我想这是你能做到的一种方法

$quantity = 8;
$remainder = $quantity % 6 ? $quantity % 6 : 0;
$discountItems = $quantity - $remainder;    
echo "10% off " . $discountItems . " items and full price on " . $remainder;
// 10% off 6 items and full price on 2
你所需要的只是魔法。
$magicPriceFunction = function ($pricePerItem, $itemCount) {
    $magicNumber = 6;
    $discount    = 0.1;

    $unhappyItemCount = $itemCount % $magicNumber;

    $total  = 0;
    $total += $pricePerItem * $unhappyItemCount;
    $total += ($itemCount - $unhappyItemCount) * (1 - $discount) * $pricePerItem;

    return $total;
};

var_dump($magicPriceFunction(100, 6));

这将告诉您除以6后是否有余数。没有余数,这是一个倍数。谢谢Pavlunya的回复。帮我把全部东西都拿出来。我感谢你的努力。再次感谢