背景信息:在我们的网站上,我们使用if语句计算商品/订单是否符合免费送货标准,以评估值 - 'ff' - 是否为真。根据所满足的条件,它会适当地设置运输。我们现在需要评估商品是否按照 _ 的顺序免费送货。最重要的是,出于其他目的,如外部饲料,必须适用另一个条件(重量必须= 0或没有价值),因为fw是免费运费。这是代码,我无法弄清楚它为什么不起作用:
if($r['freeFreight'] == 'ff' || ($r['freeFreight'] == 'fw' && ($r['weight'] == 0 || $r['weight'] == '') ) ) {
$r['shippingStandard'] = 0;
}
if语句中的条件是否与()的集合过分隔离?或者,他们只是错误地放置。例如,它应该是:
if(($r['freeFreight'] == 'ff') || ($r['freeFreight'] == 'fw' && $r['weight'] == 0 || $r['weight'] == '') ) {
$r['shippingStandard'] = 0;
}
这两个中的第二个似乎对我更合乎逻辑,因为:if($ r ['freeFreight'] =='ff') - 然后无论以下条件如何,语句都应返回true并将变量设置为0。我的逻辑正确吗?对不起这个新问题......
感谢您提供的任何帮助。
所以我想也许,基于到目前为止的答案(感谢所有帮助业余爱好者的人) - 我想我将要进行试验:
if( ($r['freeFreight'] == 'ff') || ( $r['freeFreight'] == 'fw' ) && empty( $r['weight'] ) ) {
$r['shippingStandard'] = 0;
}
计划对此进行试用,如果从根本上说是错误的,请告知。
答案 0 :(得分:2)
试试这个?
if( $r['freeFreight'] == 'ff' || ( $r[ 'freeFreight' ] == 'fw' && empty( $r['weight'] ) ) ) {
$r['shippingStandard'] = 0;
}
使用empty
可能会更清洁
如果你真的想要更多地解决它,你可以这样做
if( $r[ 'freeFreight' ] == 'ff' ) {
$r[ 'shippingStandard' ] = 0;
} elseif( $r[ 'freeFreight' ] == 'fw' && empty( $r[ 'weight' ] ) ) {
$r[ 'shippingStandard' ] = 0;
} else {
// Everything else
}
如果您想缩短变量,稍后将使用它们:
$freight = $r[ 'freeFreight' ];
$weight = isset( $r[ 'weight' ] ) ? $r[ 'weight' ] : null;
$shipping = $r[ 'shippingStandard' ]; // Or whatever you want the default value to be...
if( $freight == 'ff' || ( $freight == 'fw' && empty( $weight ) ) ) {
$shipping = 0;
}
// ... Later down the file
$r = array(
'freeFreight' => $freight,
'weight' => $weight,
'shippingStandard' => $shipping
);
我真的无法告诉文件的其余部分是什么样的,比如这是否在运行功能中,您只需返回$shipping
即可。希望这可以帮助。你应该能够移动概念来获得你想要的东西
答案 1 :(得分:-1)
您的第一个选项应与以下内容相同:
if ($r['freeFreight'] == 'ff') {
$r['shippingStandard'] = 0;
}
elseif ($r['freeFreight'] == 'fw') {
// the dual check for an empty value here is kind of redundant and unneccessary.
// I'd probably just use "if ($r['weight'])" and be done with it
if ($r['weight'] == 0) {
$r['shippingStandard'] = 0;
}
elseif ($r['weight'] == '') {
$r['shippingStandard'] = 0;
}
else {
// $r['shippingStandard'] stays whatever it was before
}
}
else {
// $r['shippingStandard'] stays whatever it was before
}
如果这个逻辑对你来说是对的,那么你应该是正确的,我print_r($r)
确保它持有你期望它持有的东西。第一个例子中的括号正是我要做的。