4

I am running a very simple if statement which works perfect until I add the two additional || (or) operators.

Here is my code:

if ($planDetails['Company']['name'] != 'company1'
|| $planDetails['PlanDetail']['name'] != 'pd-name1' 
|| $planDetails['PlanDetail']['name'] != 'pd-name2') { echo "TEST"; }

I've checked my array data and table values to ensure they are precise in the names etc.. And this is not kicking. What am I doing wrong? When I remove the additional 2 || options, the first argument works fine, so I know my logic is correct.

What in the name am I doing wrong here. Someone please set me straight!

4
  • post a var_dump($planDetails); Commented Mar 4, 2011 at 20:30
  • Matt, my dump is much too large to sensibly post here, but I already checked that. The array contains the values I need and I am echoing them out simply by: echo $planDetails['PlanDetail']['name']; gets me the correct value on the requested page. Commented Mar 4, 2011 at 20:33
  • "this is not kicking" - Which means? I interpreted that as "it does not print out TEST", which is probably not what you meant. Commented Mar 4, 2011 at 20:52
  • Yes, Eric I meant it's not working as intended. The "TEST" should only echo if no matches are found. Commented Mar 4, 2011 at 21:00

3 Answers 3

7

Well, what I read here is:

If $variable is not equal to A Or $variable is not equal to B
Then echo "TEST"

Since $variable cannot be equal to both A and B at the same time, it will always print "TEST".

Of course, the above refers to the last two conditions in your if.

Sign up to request clarification or add additional context in comments.

2 Comments

+1: It becomes clearer if you apply DeMorgan's Law and think of the statement as: If NOT($variable = A AND $variable = B). Then it's obvious that $variable cannot be both A and B simultaneously.
I'm getting a taste of this logic now. This really threw me a left curve as I now see how wrong I was. Thanks for the added clarification.
6

Your ors should be ands. The expression a != 1 || a != 2 is always true because whatever the value of a is, one or the other of the expressions will be true, so the final result will be true.

To fix, change || to &&.

I guess you made this mistake because you started with this expression and wanted to invert it:

if ($planDetails['Company']['name'] == 'company1'
|| $planDetails['PlanDetail']['name'] == 'pd-name1' 
|| $planDetails['PlanDetail']['name'] == 'pd-name2')

The simplest way to invert this expressions is as follows:

if (!($planDetails['Company']['name'] == 'company1'
|| $planDetails['PlanDetail']['name'] == 'pd-name1' 
|| $planDetails['PlanDetail']['name'] == 'pd-name2'))

Using this method, you don't need to do any complex boolean logic in your head to see that it works - it's just a simple negation of what you already know. Note that this is not the same as inverting the == to != individually. See De Morgan's Laws for more details.

Comments

6

Please refer to De Morgan's laws.

NOT (P AND Q) = (NOT P) OR (NOT Q)
NOT (P OR Q) = (NOT P) AND (NOT Q)

You have:

(NOT P) OR (NOT Q) OR (NOT R)

Which is the same as:

NOT (P AND Q) OR (NOT R) => 
NOT (P AND Q AND R)

Therefore, P, Q, R must all be false to print "TEST". If one of them is true then you will not print "TEST".

I believe you want:

NOT (P OR Q OR R)

Which would be:

(NOT P) AND (NOT Q) AND (NOT R)

OR

if (!($planDetails['Company']['name'] == 'company1'
|| $planDetails['PlanDetail']['name'] == 'pd-name1' 
|| $planDetails['PlanDetail']['name'] == 'pd-name2'))
{ 
    echo "TEST"; 
}

AKA

if ($planDetails['Company']['name'] != 'company1'
&& $planDetails['PlanDetail']['name'] != 'pd-name1' 
&& $planDetails['PlanDetail']['name'] != 'pd-name2')
{ 
    echo "TEST"; 
}

3 Comments

wow, that took me too long to type... it appears others have also posted similar answers
Dan, thanks very much for the added clarification. I see exactly what you are describing. My logic was extremely flawed.
Yes, that's why I put it first. It "reads" easier. For the fun of it, you could have put them in an array and do a contains (but worse performance): php.net/manual/en/function.in-array.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.