phpoperatorslogical-operatorsor-operator

Logical Operators, || or OR?


I remember reading a while back in regards to logical operators that in the case of OR, using || was better than or (or vice versa).

I just had to use this in my project when it came back to me, but I can't remember which operator was recommended or if it was even true.

Which is better and why?


Solution

  • There is no "better" but the more common one is ||. They have different precedence and || would work like one would expect normally.

    See also: Logical operators (the following example is taken from there):

    // The result of the expression (false || true) is assigned to $e
    // Acts like: ($e = (false || true))
    $e = false || true;
    
    // The constant false is assigned to $f and then true is ignored
    // Acts like: (($f = false) or true)
    $f = false or true;