What is the best way to use different logical operators in the same condition?

Asked

Viewed 63 times

-4

Precise using two different logical operators in the same condition. How to do it right?

I did it this way:

if($planohabilitargestao!="1000" || $planohabilitargestao!="200" || $planohabilitargestao!="10000" && $usuario=="")

Even the variable $planohabilitargestao equal to 1000, 200 and 10000 the condition is fulfilled.

Has to separate the equal operators parentheses?

  • 2

    Only with this information can not answer. Say what you want to do.

  • @mustache the above condition is correct? I wonder if I have to separate the equal operators with parentheses.

  • 2

    What is the expected result and what is the obtained result? it seems that your test fails to validate the expected ...

  • @rray Even the variable $planohabilitargestao being equal to 1000, 200 and 10000 the condition is executed.

  • Explain why this code is, $planohabilitargestao being 200, is different from 1000 logo of the true, got more confused now O.o'

  • what you have in the $user variable

  • The question is clear, the suggestion given (to use parentheses) by it is wrong, but the answer is obvious. Of course it is executed. Because you define 1000, the variable is activated by the != 200 "and" by the != 10000. You must use AND or &&, which is the same thing. So yes. If it is different from 1000 and different from 200 and different from 10000 it will be executed. So if you use 1000 it won’t run.

  • @Gladisonneuzaperosini The answer solved your problem? Do you think you can accept it? If you don’t know how, check out the [tour] how to do it. This would help a lot to indicate that the solution was useful for you and help everyone understand that. You can also vote for anything on the entire site.

Show 3 more comments

1 answer

2

Operators have equal precedence in mathematics, and the operator && which is a AND takes precedence over the operator || which is a OR. Both have left-to-right associativity, so when the operators have the same precedence, the sub-expression that appears first will be executed.

In this example there is a &&, this sub-expression will be executed first of all according to the precedence of operators, then $planohabilitargestao != "10000" && $usuario == "" is the first operation to be executed to then relate to others ||. I understand this is not what you want.

The solution to this is to use parentheses changing precedence, since this operator () has higher precedence than the others. Just do this:

($planohabilitargestao != "1000" || $planohabilitargestao != "200" || $planohabilitargestao != "10000) && $usuario == ""

Now the expression of && has as first operated the final result of the expression that is between parentheses, then all operators || are executed first and then the result is used as the operand of &&.

On the other hand it may be (after editing) that you only want this:

$planohabilitargestao != "1000" && $planohabilitargestao != "200" && $planohabilitargestao != "10000 && $usuario == ""

At least that’s what I "guessed" I wanted, I hope I got it right. I still try other options if the problem is better defined.

Precedence table.

  • 1

    It worked not... Even the variable $planohabilitargestao being equal to 1000, 200 and 10000 the condition is executed.

  • 3

    The problem is that you don’t know what you want. If you define better, I try to improve. In fact I find this odd, but the definition of the problem is that it is wrong. It is symptomatic that the code was changed in the original question. remembering that a variable cannot be equal to several values.

  • @bigown I think her problem is in the $user variable you have to see what is going on.

  • @Marconciliosouza is a possibility yes.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.