22

Is && the same as "and", and is || the same as "or" in PHP?

I've done a few tests, and it seems they behave the same. Are there any differences?

If not, are there any other PHP signs that have word equivalents and do you think it makes the code easier to read?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alex
  • 66,732
  • 177
  • 439
  • 641
  • Old VB programmer, ey? Yes, `&&` is AND, but `&` is AND (bitwise). Liekwise for `||` and `|` are OR and OR (bitwise), respectively. :: I take it back, PHP allows AND/OR words--amazing. Learn something new every day. – Brad Christie Dec 21 '10 at 17:28
  • no :) I started with Turbo Pascal, and I admit I like the pascal synthax more :) – Alex Dec 21 '10 at 17:30
  • 1
    @Alex: Ah, the days of Pascal/Delphi. How I loathe, err I mean _love_, those days... – Brad Christie Dec 21 '10 at 17:32
  • Hah... I used to prefer Pascal syntax over PHP's, but not anymore :) – Mchl Dec 21 '10 at 17:33
  • Why do nearly all PHP programmers choose the signs over the words? Just because they save 1 character when using && ? – Alex Dec 21 '10 at 17:33
  • 3
    Readability I think is mostly in the eye of the beholder in this case. I find `||` and `&&` more readable, but I'd expect others to disagree. Stick with the convention of the environment you find yourself in. – Thomas Langston Dec 21 '10 at 17:35
  • CodeIgniter [uses](http://www.codeigniter.com/userguide3/general/styleguide.html#logical-operators) `&&` and `OR`, instead of `||` because "its clarity on some output devices is low". Grin. – Gras Double May 04 '15 at 14:52

4 Answers4

38

and and or have higher lower precedence than && and ||. To be more exact && and || have higher precedence than assignment operator ( = ) while and and or have lower.

http://www.php.net/manual/en/language.operators.precedence.php

Usually it doesn't make a difference, but there are cases when not knowing about this difference can cause some unexpected behaviour. See examples here:

http://php.net/manual/en/language.operators.logical.php

Mchl
  • 61,444
  • 9
  • 118
  • 120
  • 1
    The comments on http://php.net/manual/en/language.operators.php indicate they have differing precedence relative to `=` as well. – Thomas Langston Dec 21 '10 at 17:32
  • @Thomas: which is documented in the first link in my answer as well ;) But... actually this is perhaps the most important difference. I'll add it to the answer. – Mchl Dec 21 '10 at 17:53
  • 1
    The lower-than-assignment precedence of 'or' can be useful: $page = get_page() or $page = 1; – Michał Tatarynowicz Jan 29 '13 at 03:33
  • 2
    @Pies but it's horrible, I'd rater use a ternary operator in that case or, even better, handle that inside the get_page() function itself. – o0'. Apr 15 '14 at 12:57
7

Yes, they are logically the same. (I believe "&&" and "||" are the preferred choice in the Zend coding standards, but I can't find any specific information on this, so it might all have been a dream. Or something.)

That said:

  1. "&&" and "||" are of a higher precedence than "AND" and "OR" (unlikely to ever be relevant, but you never know).

  2. A lot of other languages use "&&" and "||", rather than the textual equivalents so it might be an idea to go with this.

  3. As long as you use your choosen set of operators consistently it doesn't really matter.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
John Parker
  • 54,048
  • 11
  • 129
  • 129
  • It could make a huge difference if the precedence of "=" (assignment) is inbetween the two operators - which [is actually the case](http://php.net/manual/en/language.operators.precedence.php)! – Peter Mortensen Dec 06 '15 at 11:01
5

What bothers me is:

echo (false and false ? true : true);
// (empty/false)

You might guess there is only the possible output of "1" (true) as there is no case which could output a false... ...but it will be "" (false).

Using && as a operator in this case satifies at least my expectations:

echo (false && false ? true : true);
// 1

So, in some cases the usage matters significantly.

K G
  • 51
  • 1
  • 1
4

The difference is on the precedence. But not only compared with each other!

In most cases you won't mind it, but there are specific cases when you have to take one step back and look at the big picture. Take this, for example:

// The result of the expression (true && false) is assigned to $g
// Acts like: ($g = (true && false))
$g = true && false;

// The constant true is assigned to $h before the "and" operation occurs
// Acts like: (($h = true) and false)
$h = true and false;

var_dump($g, $h);

This will produce, respectively:

bool(false)
bool(true)

In other words, && has higher preference than =, which has higher precedence than and, as stated in http://php.net/manual/en/language.operators.precedence.php. (It is mentioned in other answers, but I think it's worth to detail, since a misuse can lead to logical errors)

I hope it may help you. You can find more at http://php.net/manual/en/language.operators.logical.php

Bruno Lima
  • 301
  • 2
  • 5