# What is the difference between "&&" and "||" and "and" and "or" in PHP? Which one to use?

30

I have this question, what is the difference between `&&` and `||` and between `and` and `or` ? Which one should I use?

asked by anonymous 22.01.2015 / 16:04

28

The difference between them is in the order of precedence of each.

# Order of precedence? But what?!

For example, let's take the mathematical expression 3 + 4 * 2 . What is the result of the expression? 14 or 11? The answer is 11 , because the `*` operator has a higher precedence than the `+` operator.

In this case of `&&` , `||` , `and` and `or` , the first two have greater precedence over the other two, otherwise, are identical.

In PHP there are also the `&` and `|` operators, but these make bit-a-bit comparisons and between references, instead of the others quoted, that perform logical operations themselves.

20.09.2017 / 17:27
19

This image illustrates well the difference between both logical operators >.

BothoperatorsresultinaBooleanvalueietrueorfalse.

And:`\$aand\$b`:Returnstrueifbothpastvaluesaretrue.

Or:`\$aand\$b`:Returnstrueif`\$a`or`\$b`aretrue.

Xor:`\$axor\$b`:Althoughyouhavenotquotedthisoperator,itissimilarto`Or`,returnstrueif`\$a`or`\$b`aretruebutnotboth.

!:`!\$a`:Unaryoperator,returnstrueif`\$a`isnottrue.

&&:`\$a&&\$b`:Binaryoperator,returnstrueifboth`\$a`and`\$b`aretrue.

## OperatorPrecedence

Asexplainedinthe mutley response , the difference between these operators is in the precedence between them, ie which operator is evaluated first and which will be evaluated next, as you can see in the following table, both `\$a || \$b` and `\$a` have priority over `\$b` and `&&` .

The following table shows the precedence of the operators, from the highest precedence in the beginning to the lowest precedence.

## Example

``//ANDvar_dump(7==7AND9>7);//TRUE,ambasasexpressõessãoverdadeirasvar_dump(7==7AND9<7);//FALSE,apenasaprimeiraexpressãoéverdadeira//ORvar_dump(7==7OR9>7);//TRUE,ambasasexpressõessãoverdadeirasvar_dump(7!=7OR9>7);//TRUE,asegundaexpressãoéverdadeiravar_dump(7!=7OR9<7);//FALSE,ambasasexpressõessãofalsas//XORvar_dump(7==7XOR9>7);//FALSE,ambasasexpressõessãoverdadeirasvar_dump(7==7XOR9<7);//TRUE,aprimeiraexpressãoéverdadeiravar_dump(7<7XOR9>7);//TRUE,asegundaexpressãoéverdadeira//!var_dump(!9<7);//TRUE,9NÃOémenorque7var_dump(!9>7);//FALSE,9émaiorque7//&&var_dump(7==7&&9>7);//TRUE,ambasasexpressõessãoverdadeirasvar_dump(7==7&&9<7);//FALSE,apenasaprimeiraexpressãoéverdadeira//||var_dump(7==7||9>7);//TRUE,ambasasexpressõessãoverdadeirasvar_dump(7!=7||9>7);//TRUE,asegundaexpressãoéverdadeiravar_dump(7!=7||9<7);//FALSE,ambasasexpressõessãofalsas``

Demo

22.01.2015 / 16:24
8

This is logical operators always results in a value of type Boolean ( `true` or `false` ) and are widely used where conditional expression is expected.

`&&` results in `true` if both of its operands are true, so it is common for these operands to be relational .

`||` results in `true` if at least one of the two operands is true.

`and` and `or` are the equivalent of previous operators but have a precedence smaller.

As the documentation shows in the first case the interpretation is this:

``````(\$e = (false || true))
``````

In the second case it looks like this:

``````((\$f = false) or true)
``````

So in the first case the conditional expression using the `||` operator is more important and executes first. In the second case the second operand functions as something additional, the important one is the previous assignment expression. This example does nothing useful, because `\$f = false` will always work and `true` will never run.

Here comes a curiosity that not everyone knows. These operators work in the form of short circuit, ie if it can already anticipate the final result it does not continue to run. In the case of `or` if the first expression is already considered true, the second does not need to be executed any more, with a `or` just one of the two expressions being true. With `and` the opposite occurs, if the first expression is false, the second does not have to be executed since it is impossible for the final result to be true, remembering that a `and` requires that both expressions must be true to result in true.

22.01.2015 / 16:21
4

The `&&` and `||` operators have higher precedence over `and` and `or` . This determines the order in which they will run. `&&` and `||` are executed before `and` and `or` .

22.01.2015 / 16:23