Note that there are some explanatory texts on larger screens.

plurals
  1. POphp syntax for if statement with multiple conditions ( `||` as well as `&&` )
    primarykey
    data
    text
    <p>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 <em>_</em>. 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:</p> <pre><code>if($r['freeFreight'] == 'ff' || ($r['freeFreight'] == 'fw' &amp;&amp; ($r['weight'] == 0 || $r['weight'] == '') ) ) { $r['shippingStandard'] = 0; } </code></pre> <p>Are the conditions overly segregated in the if statement with the sets of ()? Or, are they just incorrectly placed. For example, should it be:</p> <pre><code>if(($r['freeFreight'] == 'ff') || ($r['freeFreight'] == 'fw' &amp;&amp; $r['weight'] == 0 || $r['weight'] == '') ) { $r['shippingStandard'] = 0; } </code></pre> <p>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...</p> <p>Thanks for any help you can offer.</p> <hr> <p>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:</p> <pre><code>if( ($r['freeFreight'] == 'ff') || ( $r['freeFreight'] == 'fw' ) &amp;&amp; empty( $r['weight'] ) ) { $r['shippingStandard'] = 0; } </code></pre> <p>Planning to run trial with this, if it is fundamentally wrong, please advise.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload