php 逻辑运算符,|| 或或?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5998309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Logical Operators, || or OR?
提问by ItsPronounced
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).
我记得之前读过关于逻辑运算符的文章,在 的情况下OR
,使用||
优于or
(反之亦然)。
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?
哪个更好?为什么?
回答by Felix Kling
There is no "better" but the more common one is ||
. They have different precedenceand ||
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;
回答by Matthew Ratzloff
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.
它们用于不同的目的,实际上具有不同的运算符优先级。的&&
和||
运营商意图用于布尔条件,而and
和or
旨在用于控制流程。
For example, the following is a Boolean condition:
例如,以下是一个布尔条件:
if ($foo == $bar && $baz != $quxx) {
This differs from control flow:
这与控制流不同:
doSomething() or die();
回答by John Slegers
The difference between respectively || and OR and && and AND is operator precedence:
|| 分别的区别 and OR and && and AND 是运算符优先级:
$bool = FALSE || TRUE;
$bool = FALSE || TRUE;
- interpreted as
($bool = (FALSE || TRUE))
- value of
$bool
isTRUE
- 解释为
($bool = (FALSE || TRUE))
- 的值
$bool
是TRUE
$bool = FALSE OR TRUE;
$bool = FALSE OR TRUE;
- interpreted as
(($bool = FALSE) OR TRUE)
- value of
$bool
isFALSE
- 解释为
(($bool = FALSE) OR TRUE)
- 的值
$bool
是FALSE
$bool = TRUE && FALSE;
$bool = TRUE && FALSE;
- interpreted as
($bool = (TRUE && FALSE))
- value of
$bool
isFALSE
- 解释为
($bool = (TRUE && FALSE))
- 的值
$bool
是FALSE
$bool = TRUE AND FALSE;
$bool = TRUE AND FALSE;
- interpreted as
(($bool = TRUE) AND FALSE)
- value of
$bool
isTRUE
- 解释为
(($bool = TRUE) AND FALSE)
- 的值
$bool
是TRUE
回答by improgrammer
Source: http://wallstreetdeveloper.com/php-logical-operators/
来源: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>
回答by Zdeněk
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.
您会期望(正如您习惯于从例如 javascript 中那样)当 $this->positions() 返回 false 或 null 时,$positions 是空数组。但事实并非如此。值是 TRUE 还是 FALSE 取决于 $this->positions() 返回的内容。
If you need to get value of $this->positions() or empty array, you have to use:
如果您需要获取 $this->positions() 或空数组的值,则必须使用:
$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:
上面的例子没有按预期工作,但事实是,||
并且or
不一样......试试这个:
<?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
用 PHP 7.2.1 测试
回答by Nishu Tayal
There is nothing bad or better, It just depends on the precedence of operators. Since ||
has higher precedence than or
, so ||
is mostly used.
没有什么不好或更好,它只取决于运算符的优先级。由于||
优先级高于or
,因此||
主要使用。
回答by exahex
I don't think one is inherently better than another one, but I would suggest sticking with || because it is the default in most languages.
我不认为一个天生就比另一个好,但我建议坚持使用 || 因为它是大多数语言的默认设置。
EDIT: As others have pointed out there is indeed a difference between the two.
编辑:正如其他人指出的那样,两者之间确实存在差异。
回答by janhsh
Some languages use short-circuit, and others use full Boolean evaluation (if you know, this is similar to the directive $B
in Pascal).
一些语言使用短路,而其他语言使用完整的布尔计算(如果您知道,这类似于$B
Pascal 中的指令)。
Explanations:
说明:
function A(){
...Do something..
return true;
}
function B(){
...Do something..
return true;
}
if ( A() OR B() ) { .....
In this example the function B()
will never be executed. Since the function A()
returns TRUE, the result of the OR statement is known from the first part without it being necessary to evaluate the second part of the expression.
在这个例子中,函数B()
永远不会被执行。由于该函数A()
返回 TRUE,因此 OR 语句的结果可以从第一部分得知,而无需计算表达式的第二部分。
However with ( A() || B() )
, the second part is always evaluated regardless of the value of the first.
但是( A() || B() )
,对于第二部分,无论第一部分的值如何,始终会对其进行评估。
For optimized programming, you should always use OR
which is faster (except for the case when the first part returns false
and second part actually needs to be evaluated).
对于优化编程,您应该始终使用OR
哪个更快(除了第一部分返回false
而第二部分实际上需要评估的情况)。