2
var_dump(false and false || true);
// result: bool(false)

(false and false || true) returns false as expected.

var_dump(false and false or true);
// result: bool(true)

but (false and false or true) returns true. I have no logical explanation why this is happening.

Fabien Papet
  • 2,244
  • 2
  • 25
  • 52
Molecular Man
  • 22,277
  • 3
  • 72
  • 89
  • 1
    I suppose first place to look in is manual. Where there's clear explanation about [operators precedence](http://php.net/manual/en/language.operators.precedence.php) – Alma Do Jul 17 '14 at 07:18
  • http://php.net/manual/en/language.operators.precedence.php –  Jul 17 '14 at 07:18
  • 1
    thanks guys. I've just got it carved in my brain that `||` and `or` are equivalent for some reason – Molecular Man Jul 17 '14 at 07:21

3 Answers3

9

&& and || have higher precedence on and and or

You can see Operator precedence on PHP documentation

So

<?php 
var_dump(false and false || true);
// eq: false and (true or false) => false

var_dump(false and false or true);
// eq: (false and false) or true => true
Fabien Papet
  • 2,244
  • 2
  • 25
  • 52
1

Look at http://php.net/manual/en/language.operators.precedence.php

When we sort operators by priority, it´s:

&& > || > AND > OR

And it´s answer to your question. || has higher priority than AND.

I suppose you to use only one 'type' of logical operators, use && and || or and and or. Don´t combine them.

pavel
  • 26,538
  • 10
  • 45
  • 61
0

Even though logically || and or are the same, their precendnce is not.

|| is of a higher precedence than OR, therefore they are interpreted differently in your 2 statements. I guess if you add a bracket around, it will have the same results:

(false and (false || true));

OR

(false and (false or true));

More here

Community
  • 1
  • 1
Divi
  • 7,621
  • 13
  • 47
  • 63