Logical Operators, || or OR?

161,697

Solution 1

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;

Solution 2

They are used for different purposes and in fact have different operator precedences. The && and || operators are intended for Boolean conditions, whereas and and or are intended for control flow.

For example, the following is a Boolean condition:

if ($foo == $bar && $baz != $quxx) {

This differs from control flow:

doSomething() or die();

Solution 3

The difference between respectively || and OR and && and AND is operator precedence :

$bool = FALSE || TRUE;

  • interpreted as ($bool = (FALSE || TRUE))
  • value of $bool is TRUE

$bool = FALSE OR TRUE;

  • interpreted as (($bool = FALSE) OR TRUE)
  • value of $bool is FALSE

$bool = TRUE && FALSE;

  • interpreted as ($bool = (TRUE && FALSE))
  • value of $bool is FALSE

$bool = TRUE AND FALSE;

  • interpreted as (($bool = TRUE) AND FALSE)
  • value of $bool is TRUE

Solution 4

Source: http://wallstreetdeveloper.com/php-logical-operators/

Here is sample code for working with logical operators:

<html>

<head>
    <title>Logical</title>
</head>

<body>
    <?php
        $a = 10;
        $b = 20;
        if ($a>$b)
        {
            echo " A is Greater";
        }
        elseif ($a<$b)
        {
            echo " A is lesser";
        }
        else
        {
             echo "A and B are equal";
        }
    ?>
    <?php
        $c = 30;
        $d = 40;
        //if (($a<$c) AND ($b<$d))
        if (($a<$c) && ($b<$d))
        {
            echo "A and B are larger";
        }
        if (isset($d))
            $d = 100;
        echo $d;
        unset($d);
    ?>
    <?php
        $var1 = 2;
        switch($var1)
        {
            case 1:  echo "var1 is 1";
                     break;
            case 2:  echo "var1 is 2";
                     break;
            case 3:  echo "var1 is 3";
                     break;
            default: echo "var1 is unknown";
        }
    ?>
</body>
</html>

Solution 5

I know it's an old topic but still. I've just met the problem in the code I am debugging at work and maybe somebody may have similar issue...

Let's say the code looks like this:

$positions = $this->positions() || [];

You would expect (as you are used to from e.g. javascript) that when $this->positions() returns false or null, $positions is empty array. But it isn't. The value is TRUE or FALSE depends on what $this->positions() returns.

If you need to get value of $this->positions() or empty array, you have to use:

$positions = $this->positions() or [];

EDIT:

The above example doesn't work as intended but the truth is that || and or is not the same... Try this:

<?php

function returnEmpty()
{
  //return "string";
  //return [1];
  return null;
}

$first = returnEmpty() || [];
$second = returnEmpty() or [];
$third = returnEmpty() ?: [];

var_dump($first);
var_dump($second);
var_dump($third);
echo "\n";

This is the result:

bool(false)
NULL
array(0) {
}

So, actually the third option ?: is the correct solution when you want to set returned value or empty array.

$positions = $this->positions() ?: [];

Tested with PHP 7.2.1

Share:
161,697
ItsPronounced
Author by

ItsPronounced

Constantly learning web application development. Self taught ASP.NET and php since 2003.

Updated on December 28, 2020

Comments

  • ItsPronounced
    ItsPronounced over 3 years

    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?

  • giannis christofakis
    giannis christofakis over 10 years
    die() function will be called if doSomething() will return false or null? What if doSomething() returns true or nothing?
  • Matthew Ratzloff
    Matthew Ratzloff over 10 years
    doSomething() is evaluated as a boolean. If it returns a value PHP considers truthy (true, a non-empty string, etc.), it will not call die().
  • Matt Kieran
    Matt Kieran over 9 years
    and $e = true || $x = 'foo' will not define $x because of short-circuiting, not because of higher precedence.
  • bzeaman
    bzeaman over 9 years
    This is not 'The Best Answer !!'. Please scroll back up and take the most up voted answer for a good explanation. With the ||, B will not be called. Both operators do exactly the same, except that the precedence is differs.
  • Jens A. Koch
    Jens A. Koch almost 8 years
    "you should always use OR which is faster" Hmm, i wonder if this is true... so let's check that: 3v4l.org/5QSAA/vld#tabs 3v4l.org/PdjJP/vld#tabs The number of opcodes is the same. So it doesn't matter performance wise.
  • TextGeek
    TextGeek over 6 years
    It is also worth noting that these always return a boolean value, unlike many other languages where they return the last value checked. So in PHP (27 || 0) returns true, not 27.
  • WayFarer
    WayFarer about 6 years
    it's incorrect answer, the second example works exactly as first one
  • Zdeněk
    Zdeněk about 6 years
    @WayFarer well, it's not correct (there is an issue) but you're wrong too (|| and OR is not the same) - see my edit
  • WayFarer
    WayFarer about 6 years
    right, operators || and 'or' have different priority, so, your second example works as: (($second = returnEmpty()) or []); So, the answer to original question would be || is better, always use it, until you really understand why do you want to use 'or'
  • bishop
    bishop almost 6 years
    $positions = $this->positions() ?? []; is likely what you want.
  • jinyong lee
    jinyong lee over 5 years
    @TextGeek, "these"? 27 or 0 returns 27 for me.
  • TextGeek
    TextGeek over 5 years
    @Jānis Elmeris -- you are correct, I should have referred only to the "||" case.
  • jinyong lee
    jinyong lee over 5 years
    @TextGeek, actually, you are right that or returns boolean as well. Just its precedence is so low that it sometimes looks like it does something else. :) print 27 or 0 would print 27 because or happens after print 27. BTW, echo is not fooled – echo 27 or 0 would output 1.
  • Peter Mortensen
    Peter Mortensen over 4 years
    The link is broken.