سؤال

Context: On our website, we calculate whether an item/order meets the criteria for free shipping using an if statement evaluating if a value - 'ff' - is true. Depending on conditions met it sets the shipping appropriately. We now require the need to evaluate if an item is free shipping with the order of _. To top it off, for other purposes such as external feeds, another condition must apply (weight must = 0 or have no value), for fw to be free freight. Here is the code, that I can't figure out why it is NOT working:

if($r['freeFreight'] == 'ff' || ($r['freeFreight'] == 'fw' && ($r['weight'] == 0 || $r['weight'] == '') ) ) {
    $r['shippingStandard'] = 0;
}

Are the conditions overly segregated in the if statement with the sets of ()? Or, are they just incorrectly placed. For example, should it be:

if(($r['freeFreight'] == 'ff') || ($r['freeFreight'] == 'fw' && $r['weight'] == 0 || $r['weight'] == '') ) {
    $r['shippingStandard'] = 0;                     
}

The second of the two seems more logical to me because: if ($r['freeFreight'] == 'ff') - then regardless of the following conditions the statement should return true and set the variable to 0. Is my logic correct? Sorry for this newb question...

Thanks for any help you can offer.


So I think perhaps, based on the answers so far (thanks everybody that has chimed in to help an amateur) - I think I am going to do a trial with:

if( ($r['freeFreight'] == 'ff') || ( $r['freeFreight'] == 'fw' ) && empty( $r['weight'] ) ) { 
    $r['shippingStandard'] = 0;
}

Planning to run trial with this, if it is fundamentally wrong, please advise.

هل كانت مفيدة؟

المحلول

Try this out?

if( $r['freeFreight'] == 'ff' || ( $r[ 'freeFreight' ] == 'fw' && empty( $r['weight'] ) ) ) {
    $r['shippingStandard'] = 0;
}

using empty might be a little cleaner too

if you really want to break it out more, you could do this

if( $r[ 'freeFreight' ] == 'ff' ) {
    $r[ 'shippingStandard' ] = 0;
} elseif( $r[ 'freeFreight' ] == 'fw' && empty( $r[ 'weight' ] ) ) {
    $r[ 'shippingStandard' ] = 0;
} else {
    // Everything else
}

If you want to shorten the variables, and will be using them later:

$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
);

I really cant tell how the rest of the file looks, like if this is in a function to get shipping, you could simply just return $shipping. Hope this helps. You should be able to move the concepts around to get what you want

نصائح أخرى

your first option should should be identical to the following:

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
}

if that logic looks right to you, then you should be right on and I'd print_r($r) to make sure it's holding what you expect it to be holding. Your parenthesis in the first example is exactly how I would do it.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top