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.
Only with this information can not answer. Say what you want to do.
– Maniero
@mustache the above condition is correct? I wonder if I have to separate the equal operators with parentheses.
– Gladison Neuza Perosini
What is the expected result and what is the obtained result? it seems that your test fails to validate the expected ...
– rray
@rray Even the variable $planohabilitargestao being equal to 1000, 200 and 10000 the condition is executed.
– Gladison Neuza Perosini
Explain why this code is,
$planohabilitargestao
being 200, is different from 1000 logo of thetrue
, got more confused now O.o'– rray
what you have in the $user variable
– Marco Souza
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.– Inkeliz
@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.
– Maniero