2

What is the difference between "||" and "or"?

I tried doing this in PHP:

$a = false || true;
$b = false or true;

var_dump($a);
var_dump($b);

You obviously think that the result would be true for $a and true for $b, but the result I got is this:

boolean true
boolean false
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Khalid
  • 4,730
  • 5
  • 27
  • 50
  • 2
    In a nutshell, `||` has precedence over `OR`. Yet, they both basically do the same thing. – Funk Forty Niner Jul 22 '14 at 22:21
  • This is very clearly stated in the manual. See [PHP: Logical Operators](http://php.net/manual/en/language.operators.logical.php). – Boaz Jul 22 '14 at 22:22

1 Answers1

6

"||" has a greater precedence than "or".

An example (from the PHP documentation):

<?php
    // "||" has a greater precedence than "or"
    $e = false || true; // $e will be assigned to (false || true) which is true
    $f = false or true; // $f will be assigned to false
    var_dump($e, $f);
?>

Read more here: Logical Operators

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Matt Kent
  • 1,145
  • 1
  • 11
  • 26