[Solved] PHP If Both Variables are 0 [closed]


A common way to visualise logical conditions like this is as a truth table:

  | 0 | 1
--+------
0 |   |  
--+---+--
1 |   |  

Across the top, we have the possible values of $QuantityOrdered; down the side, the values of $QuantityShipped. In the grid, we put the result of the expression; I’ll use T for true and F for false.

For your current condition, $QuantityOrdered != '0' && $QuantityShipped != '0', we get this:

  | 0 | 1
--+------
0 | F | F
--+---+--
1 | F | T

We are using 1 to mean “not 0”, so the only combination that’s true in our table is where both variables are 1.

But what you actually wanted was this:

  | 0 | 1
--+------
0 | F | T
--+---+--
1 | T | T

You wanted the condition to be true in all cases except for one, the case where both variables are 0.

One way to write this would be to start with the opposite. For $QuantityOrdered == '0' && $QuantityShipped == '0' we get this:

  | 0 | 1
--+------
0 | T | F
--+---+--
1 | F | F

So we could write ! ( $QuantityOrdered == '0' && $QuantityShipped == '0' ).

It turns out this is equivalent to $QuantityOrdered != '0' || $QuantityShipped != '0'. Fill in the table, and you’ll see.

This equivalence, where “NOT (a AND b)” is equivalent to “(NOT a) OR (NOT b)”, is called ”De Morgan’s Law”.

solved PHP If Both Variables are 0 [closed]