Logical Operators, || or or?

I remember some time ago with respect 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 don’t remember which operator was recommended, or if that was even true.

Which is better and why?

+64
operators php logical-operators
May 13 '11 at 10:15
source share
8 answers

There is no "best", but the most common is || . They have different priority and || will work as expected.

See also: Logical operators (from this we select the following example):

 // 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; 
+97
May 13 '11 at 22:17
source share

They are used for different purposes and in fact have different operator priorities. Operators && and || are for boolean conditions, while and and or are for control flow.

For example, the following condition is Boolean:

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

This is different from the control flow:

 doSomething() or die(); 
+32
May 13 '11 at 10:20
source share

The difference between respectively || and OR and && and AND operator priority :

$bool = FALSE || TRUE;

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



$bool = FALSE OR TRUE;

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



$bool = TRUE && FALSE;

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



$bool = TRUE AND FALSE;

  • interpreted as (($bool = TRUE) AND FALSE)
  • $bool value is TRUE
+11
Jan 18 '16 at 6:38
source share

Source: http://bit.ly/1hxDmVR

Here is an example 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> 
+5
May 7 '14 at 18:03
source share

I do not think that it is inherently better than the other, but I would suggest sticking to || because it is the default value in most languages.

EDIT: As others have pointed out, there really is a difference between the two.

0
May 13, '11 at 22:17
source share

There is nothing bad or better, it just depends on the priority of the operators. Since || has a higher priority than or , therefore || used mainly.

0
Sep 29 '11 at 17:13
source share

I know this is an old topic, but still. I just met a problem in the code that I am debugging at work, and maybe someone might have a similar problem ...

Let's say the code is as follows:

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

You would expect (as you are used to, for example, from javascript) when $ this-> position () returns false or null, $ position is an empty array. But this is not so. The value TRUE or FALSE depends on what $ this-> position () returns.

If you need to get the value of $ this-> position () or an empty array, you should use:

 $positions = $this->positions() or []; 
0
Nov 13 '17 at 13:37 on
source share

Some languages ​​use either a short circuit, or others use a full logical evaluation (if you know, this is similar to the $B directive in pascal)

Explanations:

 function A(){ ...do somethings.. return true; } function B(){ ...do somethings.. return true; } if ( A() OR B() ) { ..... 

In this example, function B() will never be executed. Since the A() function returns TRUE, the result of the OR operation is known from the first part without the need to evaluate the second part of the expression.

However, with ( A() || B() ) second part is always evaluated regardless of the value of the first.

For optimized programming, you should always use OR , which is faster (unless the first part returns false and the second part needs to be evaluated).

-3
Oct 05 '14 at 2:54 on
source share



All Articles