促销码为"MAG20",商品码为"99"或商品码为"77"或商品码为"44"..那就做点什么。
(促销码相同,产品列表也相同,但很长)
if ($promocode=="MAG20" && $productID=="44" && || $productID=="77") {
// wahay! run function
} else {
// no coupon for you
}
我希望如果促销MAG20和CODE是99或CODE是77将工作& && &|| -也有更好的方法来做这个,因为括号将是大的,30+产品。
你应该把你所有的名字组成一个数组
$productIDs = array(10, 20, 30);
则if函数
if($promocode== "MAG20" && in_array($productID, $productIDs))
你有一个ID列表和一个简短的if语句
如果你有太多的产品,那么最好有一个包含产品和相应优惠券代码的sql表。这是一种更好更干净的方法。在单个条件语句中设置30个条件不仅会降低应用程序的速度,而且还非常难以管理。
因此,您可以有一个优惠券表、一个产品表和一个coupons_to_products表,并检查最终表,以确定优惠券是否真的有效。
如果有效的产品id会改变或者它们很多,则使用数组
$validProductIDs = array(44, 77, 104, 204); //Up to you how you populate this array
if ($promocode == "MAG20" && in_array($productID, $validProductIDs)) {
// wahay! run function
} else {
// no coupon for you
}
使用
if ($promocode=="MAG20" and ($productID=="44" or $productID=="77"))
// wahay! run function
} else {
// no coupon for you
}
我会这样做:
if ($promocode=="MAG20" && ($productID=="44" || $productID=="77"))
看一下数组数据类型。根据您的数组设计,您可以使用isset(),/array_key_exists()或in_array()来进行检查。
不知道为什么认为连续的&& ||
应该工作。这是什么意思呢?
无论如何,你想要的是:
$promocode=="MAG20" && ($productID=="44" || $productID=="77")
您必须将$productID=="44" || $productID=="77"
分组,否则,由于AND具有更高的优先级,它将被计算为
($promocode=="MAG20" && $productID=="44") || $productID=="77"
如果您必须测试大量id,我建议使用某种查找表:
$productIDs = array('44', '77', ...);
$productIDs = array_flip($productIDs);
if($promocode=="MAG20" && isset($productIDs[$productID])) {
}