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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog