0

如果选择的国家/地区与特定的乘法相匹配,我使用以下 if 语句将乘法添加到总数中,出于某种奇怪的原因,尽管无论选择哪个国家/地区,所有语句都以 100 万欧元的运费结束。

知道有什么问题吗?

if($address['country_id']== '96' or '125')
    {
    $shipp_cost=$shipp_cost * 1;
    }
if($address['country_id']== '162')
    {
    $shipp_cost=$shipp_cost * 1.6;
    }
if($address['country_id']=='133' or '129' or '17' or '230' or '18' or '146' or '168' or '130' or '188' or '184' or '206' or '209' or '44' or '149' or '26' or '100' or '32' or '196' or '107' or '116' or '114' or '113' or '221' or '61' or '57' or '99' or '173')
    {
    $shipp_cost=$shipp_cost * 1.67;
    }
if($address['country_id']=='169' or '13' or '228' or '46' or '118' or '55' or '45' or '158' or '108')
    {
    $shipp_cost=$shipp_cost * 1.8;
    }
if($address['country_id']=='14' or '195' or '201' or '16' or '29' or '223' or '25' or '224' or '31' or '8' or '211' or '5' or '50' or '205' or '27' or '222' or '234' or '204' or '56' or '122' or '159' or '87' or '88' or '170' or '132' or '94' or '97' or '139' or '161' or '163' or '124' or '121' or '142' or '182' or '189' or '70' or '68' or '194' or '71' or '134' or '150' or '84' or '157' or '66')
    {
    $shipp_cost=$shipp_cost * 2;
    }
if($address['country_id']=='38' or '190' or '215' or '214' or '136' or '63' or '213' or '212' or '199' or '191' or '72' or '203' or '144' or            '53' or '83' or '160' or '218' or '11' or '171' or '21' or '20' or '227' or '111' or '110' or '172' or '24' or '101' or '126' or '103' or '104' or '232' or '33')
    {
    $shipp_cost=$shipp_cost * 2.2;
    }
if($address['country_id']=='2' or '98' or '102' or '105' or '219' or '220' or '175' or '60' or '92' or '81' or '77' or '198' or '74' or '73' or '202' or '165' or '140' or '141' or '233' or '138' or '235' or '137' or '148' or '152' or '231')
    {
    $shipp_cost=$shipp_cost * 2.4;
    }
if($address['country_id']=='151' or '7' or '65' or '4' or '19' or '135' or '131' or '22' or '23' or '28' or '64' or '1' or '119' or '120' or '115' or '40' or '3' or '208' or '217' or '181' or '186' or '176' or '216' or '229' or '193' or '155' or '156')
    {
    $shipp_cost=$shipp_cost * 2.6;
    }
if($address['country_id']=='153' or '154' or '147' or '80' or '183' or '93' or '90' or '86' or '15' or '164' or '180' or '109' or '185' or '76' or '41' or '39' or '35' or '178' or '30' or '26' or '225' or '9' or '207' or '51' or '58' or '59' or '69' or '82' or '47' or '210' or '42')
    {
    $shipp_cost=$shipp_cost * 2.8;
    }
if($address['country_id']=='95' or '37' or '239' or '117' or '123' or '238' or '145' or '34' or '226' or '106' or '143' or '43' or '62' or '52' or '85' or '67' or '200' or '179' or '91' or '192' or '78')
    {
    $shipp_cost=$shipp_cost * 3;
    }
if($address['country_id']=='75' or '127' or '48' or '128' or '10' or '197')
    {
    $shipp_cost=$shipp_cost * 3.2;
    }
if($address['country_id']=='187')
    {
    $shipp_cost=$shipp_cost * 3.4;
    }
if($geo_zone=='166' or '174')
    {
    $shipp_cost=$shipp_cost * 3.6;
    }
if($geo_zone=='54')
    {
    $shipp_cost=$shipp_cost * 3.8;
    }
if($geo_zone=='12')
    {
    $shipp_cost=$shipp_cost * 4.8;
    }
if($geo_zone=='6' or '79' or '89' or '167' or '177' or '49')
    {
    $shipp_cost=$shipp_cost * 6;
    }
4

5 回答 5

3

您的if陈述不会像您期望的那样:

if($address['country_id']== '96' or '125')
{
    $shipp_cost=$shipp_cost * 1;
}

在这里,条件将始终评估为TRUE

您需要以下内容:

if($address['country_id'] == '96' or $address['country_id'] == '125')
{
$shipp_cost = $shipp_cost * 1;
}

但是,如果您需要检查很多元素,则使用in_array更好:

将数字存储在数组中,然后使用以下命令检查给定元素是否在您定义的数组中:

$arr = array('133','129','17','230','18','146','168','130','188','184','206','209','44','149','26','100','32','196','107','116','114','113','221','61','57','99','173');

if (in_array($address['country_id'], $arr)) {
    //YAY
}

elseif作为一个不相关的旁注,我认为在这种情况下使用语句会更好。

elseif,顾名思义,是if和的组合else。与 else 一样,它扩展了一个if语句以执行不同的语句,以防原始if表达式的计算结果为FALSE。但是,与 不同的是,仅当条件表达式的计算结果为 时else,它才会执行该替代表达式。elseifTRUE

就像是:

if(condition) {    
   //code      
}

elseif (condition) {
    //code
}

elseif (condition) {
    //code
}

...

第一个计算结果为 TRUE 的 elseif 表达式(如果有)将被执行。这样,您可以防止所有if语句被执行。

希望这可以帮助!

于 2013-08-23T11:58:20.567 回答
1

你应该使用如下:

if($address['country_id']== '96' or $address['country_id'] == '125') {
    $shipp_cost=$shipp_cost * 1;
} elseif($address['country_id']== '162') {
    $shipp_cost=$shipp_cost * 1.6;
} elseif(...) {
...

作为替代switch case

switch($address['country_id']) 
{
   case 96:
   case 125:
      $shipp_cost=$shipp_cost * 1;
      break;
   case 162:
      $shipp_cost=$shipp_cost * 1.6;
}
于 2013-08-23T11:58:24.880 回答
0

你的问题是,你认为if($address['country_id']== '96' or '125')会测试($address['country_id']== '96') OR ($address['country_id']== '125')

那是错误的。
因为==比 具有更高的优先级OR,所以它解析为这样的东西:if(($address['country_id'] == '96') or ('125')),而第二个括号,即('125'),解析为true,因此每个if条件都被评估。

“更高优先级”仅仅意味着它将比具有较低优先级的运算符更紧密地绑定它的运算符。
是PHP的运算符优先级表供您参考。

于 2013-08-23T12:18:25.867 回答
0

代替

if($address['country_id']== '96' or '125')

尝试

if($address['country_id']== '96' or $address['country_id'] == '125')

或者看看http://php.net/manual/en/control-structures.switch.php

于 2013-08-23T12:03:26.737 回答
0

你应该试试这个...

if($address['country_id']== '96' or '125')
    {
    $shipp_cost=$shipp_cost * 1;
    }
 elseif($address['country_id']== '162')
    {
    $shipp_cost=$shipp_cost * 1.6;
    }
 elseif($address['country_id']=='133' or '129' or '17' or '230' or '18' or '146' or '168' or '130' or '188' or '184' or '206' or '209' or '44' or '149' or '26' or '100' or '32' or '196' or '107' or '116' or '114' or '113' or '221' or '61' or '57' or '99' or '173')
    {
    $shipp_cost=$shipp_cost * 1.67;
    }
elseif($address['country_id']=='169' or '13' or '228' or '46' or '118' or '55' or '45' or '158' or '108')
    {
    $shipp_cost=$shipp_cost * 1.8;
    }
elseif($address['country_id']=='14' or '195' or '201' or '16' or '29' or '223' or '25' or '224' or '31' or '8' or '211' or '5' or '50' or '205' or '27' or '222' or '234' or '204' or '56' or '122' or '159' or '87' or '88' or '170' or '132' or '94' or '97' or '139' or '161' or '163' or '124' or '121' or '142' or '182' or '189' or '70' or '68' or '194' or '71' or '134' or '150' or '84' or '157' or '66')
    {
    $shipp_cost=$shipp_cost * 2;
    }
elseif($address['country_id']=='38' or '190' or '215' or '214' or '136' or '63' or '213' or '212' or '199' or '191' or '72' or '203' or '144' or            '53' or '83' or '160' or '218' or '11' or '171' or '21' or '20' or '227' or '111' or '110' or '172' or '24' or '101' or '126' or '103' or '104' or '232' or '33')
    {
    $shipp_cost=$shipp_cost * 2.2;
    }
elseif($address['country_id']=='2' or '98' or '102' or '105' or '219' or '220' or '175' or '60' or '92' or '81' or '77' or '198' or '74' or '73' or '202' or '165' or '140' or '141' or '233' or '138' or '235' or '137' or '148' or '152' or '231')
    {
    $shipp_cost=$shipp_cost * 2.4;
    }
elseif($address['country_id']=='151' or '7' or '65' or '4' or '19' or '135' or '131' or '22' or '23' or '28' or '64' or '1' or '119' or '120' or '115' or '40' or '3' or '208' or '217' or '181' or '186' or '176' or '216' or '229' or '193' or '155' or '156')
    {
    $shipp_cost=$shipp_cost * 2.6;
    }
elseif($address['country_id']=='153' or '154' or '147' or '80' or '183' or '93' or '90' or '86' or '15' or '164' or '180' or '109' or '185' or '76' or '41' or '39' or '35' or '178' or '30' or '26' or '225' or '9' or '207' or '51' or '58' or '59' or '69' or '82' or '47' or '210' or '42')
    {
    $shipp_cost=$shipp_cost * 2.8;
    }
elseif($address['country_id']=='95' or '37' or '239' or '117' or '123' or '238' or '145' or '34' or '226' or '106' or '143' or '43' or '62' or '52' or '85' or '67' or '200' or '179' or '91' or '192' or '78')
    {
    $shipp_cost=$shipp_cost * 3;
    }
elseif($address['country_id']=='75' or '127' or '48' or '128' or '10' or '197')
    {
    $shipp_cost=$shipp_cost * 3.2;
    }
elseif($address['country_id']=='187')
    {
    $shipp_cost=$shipp_cost * 3.4;
    }
elseif($geo_zone=='166' or '174')
    {
    $shipp_cost=$shipp_cost * 3.6;
    }
elseif($geo_zone=='54')
    {
    $shipp_cost=$shipp_cost * 3.8;
    }
elseif($geo_zone=='12')
    {
    $shipp_cost=$shipp_cost * 4.8;
    }
elseif($geo_zone=='6' or '79' or '89' or '167' or '177' or '49')
    {
    $shipp_cost=$shipp_cost * 6;
    }
于 2013-08-23T12:05:14.097 回答