markb
markb

Reputation: 1295

Correctly using "&&", "||", and "!=" in 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;

Upvotes: 0

Views: 655

Answers (3)

Ja͢ck
Ja͢ck

Reputation: 173642

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]);
        }
    }
}

Upvotes: 0

fedorqui
fedorqui

Reputation: 290025

It is normally good to format the code to see it more clearly.

From:

if( !strncmp($method_id,'OPTION', 6) && ( ($method_id != 'OPTION_5') || ($method_id != 'OPTION_12') ) )

to:

if(

  !strncmp($method_id,'OPTION', 6)

  &&

  ( 
      ($method_id != 'OPTION_5')
      ||
      ($method_id != 'OPTION_12') 
  ) 

)

This part is always true:

  ($method_id != 'OPTION_5')
  ||
  ($method_id != 'OPTION_12') 

Because one of those will always be true, and true || false === false || true === true.

So the if will be true iff !strncmp($method_id,'OPTION', 6) === true, meaning strncmp($method_id,'OPTION', 6)===false.

Check the logic you want to use for it.

Upvotes: 1

kainaw
kainaw

Reputation: 4334

You have some logic nonsense. Look at the or in the second part. If method_id is not 5 or method_id is not 12. Well, if it is anything but 5 or 12, you get a true out of that. If it is 5, you get "false or true" which is true. If it is 12, you get "true or false" which is true. So, the entire second half is always true. Perhaps you meant && instead of || or you want == instead of !=.

Upvotes: 4

Related Questions