Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 不同条件下If语句中的计数值';s结果_Php - Fatal编程技术网

Php 不同条件下If语句中的计数值';s结果

Php 不同条件下If语句中的计数值';s结果,php,Php,每购买5种产品的客户将获得2种产品的奖金 然而,每位购买10种产品的客户将获得6种产品的奖金 现在的问题是,如果有客户购买了26种产品,该怎么办 因此,条件是: 顾客购买 (10 x 2) //get 6 products x 2 = 12 products bonus/free + (5 x 1) // get 2 products x 1 = 2 products bonus/free 到目前为止,我的情况如下: if ($quatity > 4 &&

每购买5种产品的客户将获得2种产品的奖金

然而,每位购买10种产品的客户将获得6种产品的奖金

现在的问题是,如果有客户购买了26种产品,该怎么办

因此,条件是:

顾客购买

 (10 x 2) //get 6 products x 2 = 12 products bonus/free 

   + 

 (5 x 1) // get 2 products x 1 = 2 products bonus/free
到目前为止,我的情况如下:

if ($quatity > 4 && $quatity < 10){
    echo "Congrats, you get 2 products free"
}

if ($quatity > 9){
    echo "Congrats, you get 6 products free"
}
if($quantity>4&&$quantity<10){
echo“恭喜你,你可以免费获得2种产品”
}
如果($quantity>9){
echo“恭喜你,你可以免费获得6种产品”
}
如果客户购买的产品超过5个或10个,那么上面的代码对我来说可以正常工作

但正如我前面提到的,如果客户购买了10种以上的产品,比如26种产品或200种产品,该怎么办?我是否应该手动编写if函数,例如
if($quatity>9){}
if($quatity>19){}
if($quatity>29){}
if($quatity>39){}
,等等

有人能想出一个简单的方法吗


提前谢谢

此代码将为您提供所需的内容

它将分解10项产品奖金,然后查看是否还有5项奖金可以发放

$tenBonus = (int)($quatity / 10);
$fiveBonus = $quatity % 10 > 4 ? 1 : 0;
$bonus = ($tenBonus * 5) + ($fiveBonus * 2);
if($bonus > 0) {
    echo "Congrats, you get ". $bonus ." products free";
}

用户的表格在哪里?并在处理用户操作时添加更多代码。
如果($quatity>4&&$quatity<10)
这将仅在大于4且小于10时匹配。它不检查它是否等于或小于/大于。如果($quatity>=4&&$quatity@Fred ii,你需要做
,谢谢你漂亮的旁注。我会编辑它。:)不客气,艾丽莎。非常感谢!这给了我我所需要的,但在回应之前我需要做一个if声明。如何做到这一点:
如果($bonus){echo“恭喜你,你得到了”$bonus.“免费产品”}
??只是想看看有没有奖金。提前谢谢。你能给我解释一下这是什么意思吗?1:0
这样我就可以知道你是怎么做到的,请。。非常感谢。