dsgs8208 2013-03-20 01:37
浏览 117
已采纳

在PHP中正确使用“&&”,“||”和“!=”

I'm not too sure about PHP but I normally took it was like math and the brackets are done before anything else.

I have this:

if( !strncmp($method_id,'OPTION', 6) && ( ($method_id != 'OPTION_5') || ($method_id != 'OPTION_12') ) )
    unset( $available_methods[ $method_id ] );

Where $method_id is equal to OPTION_ 1 through to 12.

Effectively if option 5 and 12 are available unset everything except those two.

QUESTION Why doesnt the above work.


EDIT

So I tried simplifying it but I think I made it harder to understand.

This is a shipping loop. The available methods are ship:REGULAR_LOCAL, ship:EXPRESS_LOCAL, ship:PLAT_LOCAL, ship:REGULAR_INT, ship:EXPRESS_INT, ship:PLAT_INT, and FREE_SHIPPING [where LOCAL is within shipping country, and INT is international].

When an order goes over $100, FREE_SHIPPING automatically kicks in, but I also want to have either ship:EXPRESS_LOCAL or ship:EXPRESS_INT present depending on where the customer is from.

if( !strncmp($method_id,'ship:', 5) && ( ($method_id != 'ship:EXPRESS_LOCAL') || ($method_id != 'ship:EXPRESS_INT') ) )
    unset( $available_methods[ $method_id ] );

This should return with only FREE_SHIPPING, and either ship:EXPRESS_LOCAL or ship:EXPRESS_INT.

LOOP

if( isset( $available_methods['FREE_SHIPPING'] ) ) {
    foreach( $available_methods as $method_id => $method ) {
        if( !strncmp( $method_id, 'ship:', 5 ) && ( ($method_id != 'ship:EXPRESS_LOCAL') && ($method_id != 'ship:EXPRESS_INT') ) )
            unset( $available_methods[ $method_id ] );
    }
}   
return $available_methods;
  • 写回答

3条回答 默认 最新

  • doupo1865 2013-03-20 02:13
    关注

    Your updated question should contain the correct code already; this is how I would have written it:

    $localOrInt = array('ship:EXPRESS_LOCAL', 'ship:EXPRESS_INT');
    
    if (isset($available_methods['FREE_SHIPPING'])) {
        foreach ($available_methods as $method_id => $method) {
            if (!strncmp($method_id, 'ship:', 5) && !in_array($method_id, $localOrInt)) {
                unset($available_methods[$method_id]);
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥20 MATLAB绘制两隐函数曲面的交线
  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流
  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃