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

Asked

Viewed 37,758 times

37

I have that question, what’s the difference between && and || and between and and or? Which one should I use?

4 answers

33


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

Order of precedence? But what?!

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

In that case of &&, ||, and and or, the first two take greater precedence over the other two, otherwise they are identical.

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

  • 4

    +1 for the explanation of precedence. I’ve heard users who use ORinstead of || just because it’s prettier.

  • 3

    Just adding: In giving the following example, you notice a small difference between the results: var_dump(1 or 2) is different from var_dump(1 || 2). The first returns int(1) and the second returns bool(true)

  • Use OR and AND also facilitates reading the code for laymen. But this difference of results I did not know, first because I know nothing of PHP. Hehe

  • @Wallacemaxters: at least in PHP 5.4, var_dump(1 or 2) prints bool(true). A pity that this feature, so useful and used in Python for example, does not work in PHP

22

This image illustrates the difference between the two logical operators.

inserir a descrição da imagem aqui

Both operators result in a boolean value, i.e., true(true) or false(false).

And: $a and $b: Returns true if both past values are true.

Or: $a and $b: Returns true if $a or $b are true.

Xor: $a xor $b: Although you have not quoted this operator, it is similar to Or, returns true if $a or $b are true but not both.

!: ! $a: Unary operator, returns true if $a is not true.

&&: $a && $b: Binary operator, returns true if both $a how much $b are true.

||: $a || $b: Binary operator, returns true if $a or $b are true.

Precedence of Operators

As explained in Mutley’s response, the difference between these operators is in the precedence between them, or whatever the operator is evaluated first and which one will be evaluated next, as you can see in the following table, both && as || have priority over And and Or.

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

inserir a descrição da imagem aqui

Example

// AND
var_dump(  7 == 7  AND 9 > 7  );  // TRUE, ambas as expressões são verdadeiras
var_dump(  7 == 7  AND 9 < 7  );  // FALSE, apenas a primeira expressão é verdadeira

// OR
var_dump(  7 == 7  OR 9 > 7  );   // TRUE, ambas as expressões são verdadeiras
var_dump(  7 != 7  OR 9 > 7  );   // TRUE, a segunda expressão é verdadeira
var_dump(  7 != 7  OR 9 < 7  );   // FALSE, ambas as expressões são falsas

// XOR
var_dump(  7 == 7  XOR 9 > 7  );  // FALSE, ambas as expressões são verdadeiras
var_dump(  7 == 7  XOR 9 < 7  );  // TRUE, a primeira expressão é verdadeira
var_dump(  7 < 7  XOR 9 > 7  );   // TRUE, a segunda expressão é verdadeira

// !
var_dump(  ! 9 < 7  );   // TRUE, 9 NÃO é menor que 7
var_dump(  ! 9 > 7  );   // FALSE, 9 é maior que 7

// &&
var_dump(  7 == 7  && 9 > 7  );   // TRUE, ambas as expressões são verdadeiras
var_dump(  7 == 7  && 9 < 7  );   // FALSE, apenas a primeira expressão é verdadeira

// ||
var_dump(  7 == 7  || 9 > 7  );   // TRUE, ambas as expressões são verdadeiras
var_dump(  7 != 7  || 9 > 7  );   // TRUE, a segunda expressão é verdadeira
var_dump(  7 != 7  || 9 < 7  );   // FALSE, ambas as expressões são falsas

Demo

Reference: Comparison operators, logical operators and operator precedence in PHP

  • 1

    +1 for the tables

8

This is logical operators always result in a boolean value (true or false) and are often used where a conditional expression is expected.

&& results in true if his two operands are true, therefore it is common that these operands are relational.

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

and and or are equivalent to previous operators but has a precedence minor.

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

($e = (false || true))

In the second case it’s like this:

(($f = false) or true)

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

Here comes a curiosity that not everyone knows. These operators work in short-circuit form, that is, if he can already anticipate the final result he does not continue running. In the case of the or if the first expression is already considered true, the second no longer needs to be executed, with a or just one of the two expressions is true. With the and the opposite occurs, if the first expression is false, the second does not need 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.

  • 1

    I think this answer was the only one that addressed the "short circuit" operation of the operators if (existe() and Insert()) .. this is a lot to use in java to check if the object is an instance of X and only then to call a method, all within a single if +1

  • 1

    @Isvaldofernandes: the logic is correct, but his example confuses: in the case of a hypothetical existe() and insert(), we would use the OR, and not AND: only insert if there is no.

4

Operators && and || takes precedence over and and or. This determines the order in which they will be executed. && and || are executed before and and or.

Link to the Manual

Browser other questions tagged

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