[英]Why does if($x = 1 && $x == 1) throw error but if ($x == 1 && $x = 2) doesn't in php?
In php the following code gives a warning of undefined variable $x :在 php 中,以下代码给出了未定义变量 $x的警告:
if($x = 1 && $x == 1)
I thought it was equivalent to if( ($x = 1) && ($x == 1) )
, but that's not the case.我认为它等同于if( ($x = 1) && ($x == 1) )
,但事实并非如此。 I've been told, it's because &&
has higher precedence than =
, which causes the expression to get converted to:有人告诉我,这是因为&&
的优先级高于=
,这导致表达式转换为:
if($x = (1 && ($x == 1)))
So far so good, but now consider:到目前为止一切顺利,但现在考虑:
$x=1; if($x == 1 && $x = 2)
This doesn't throw error.这不会引发错误。 Why doesn't it get converted to:为什么不转换为:
$x=1; if( (($x == 1) && $x) = 2 )
I've been told thats due to =
being right assosiative, buthttps://www.php.net/manual/en/language.operators.precedence.php says When operators have equal precedence their associativity decides how the operators are grouped.有人告诉我那是因为=
是正确的关联性,但是https://www.php.net/manual/en/language.operators.precedence.php说当运算符具有相同的优先级时,它们的关联性决定了运算符的分组方式。 . . Here we have =
, &&
and ==
all being of different precedence.这里我们有=
, &&
和==
都具有不同的优先级。
PS;附言; My actual code is if($result != false && $res = $stmt->get_result())
, which has been copied from some other reputable source, so seems like not using unneeded parenthesis is common in php.我的实际代码是if($result != false && $res = $stmt->get_result())
,它是从其他一些有信誉的来源复制的,所以似乎不使用不必要的括号在 php 中很常见。
I've played with several conditions and below is what I've got.我玩过几个条件,下面是我所得到的。
First, let's consider that we init $x
before if
statements to avoid undefined variable notice.首先,让我们考虑在if
语句之前初始化$x
以避免未定义变量通知。
Second, let's confirm theprecedence for operators:其次,让我们确认运算符的优先级:
==
is applied 1st ==
第一次应用&&
is applied 2nd &&
第二次应用=
is applied 3rd =
第三次应用This returns true
:这将返回true
:
$x = 1;
if ($x = 1 && $x == 1) {
echo 'true';
} else {
echo 'false';
}
It goes like ($x = (1 && ($x == 1)))
-> ($x = (1 && true))
-> ($x = true)
-> true
.它就像($x = (1 && ($x == 1)))
-> ($x = (1 && true))
-> ($x = true)
-> true
。
If we compare $x
to another value than the assigned one we will get false
:如果我们将$x
与另一个值而不是分配的值进行比较,我们将得到false
:
$x = 1;
if ($x = 2 && $x == 2) {
echo 'true';
} else {
echo 'false';
}
It goes like ($x = (2 && ($x == 2)))
-> ($x = (2 && false))
-> ($x = false)
-> false
.它就像($x = (2 && ($x == 2)))
-> ($x = (2 && false))
-> ($x = false)
-> false
。
The last one returns true
:最后一个返回true
:
$x = 1;
if ($x == 1 && $x = 2) {
echo 'true';
} else {
echo 'false';
}
It goes like ((($x == 1) && $x) = 2)
-> ((true && $x) = 2)
-> (true = 2)
-> true
.它就像((($x == 1) && $x) = 2)
-> ((true && $x) = 2)
-> (true = 2)
-> true
。
The last comparison can't be interpreted by PHP so it's an approximate view.最后一个比较不能被 PHP 解释,所以它是一个大概的视图。
It looks like the last action (true = 2)
totally depends on the left operand .看起来最后一个动作(true = 2)
完全取决于左操作数。 If we put $x = 2;
如果我们把$x = 2;
we will get (false = 2)
-> false
.我们将得到(false = 2)
-> false
。
I'm not sure about the last one and here is the only place were some mistakes can happen.我不确定最后一个,这是唯一可能发生错误的地方。
Otherwise, it looks like precedence works as expected.否则,看起来优先级按预期工作。
Anyway, I always put parenthesis for an assignment action inside if
operator (especially inside ternary if) to be sure that I will get what I expect.无论如何,我总是在if
运算符(尤其是在三元 if 中)中为赋值操作加上括号,以确保我会得到我期望的结果。 I don't think this affects performance or readability too much, but it may prevent some logical errors.我不认为这对性能或可读性有太大影响,但它可能会防止一些逻辑错误。
UPDATE :更新:
Concering your code if($result != false && $res = $stmt->get_result())
it's not correct to compare it to if($x == 1 && $x = 2)
because in your code are two different variables.关于您的代码if($result != false && $res = $stmt->get_result())
将其与if($x == 1 && $x = 2)
进行比较是不正确的,因为在您的代码中有两个不同的变量.
In this case logical operator will not call the second part at all if the fisrt one is false
, see the 1st example here在这种情况下,如果第一个为false
,逻辑运算符根本不会调用第二部分,请参见此处的第一个示例
UPDATE-2 :更新 2 :
After the discussion under this answer we can see that the last conditions ($x == 1 && $x = 2)
work like like (($x == 1) && ($x = 2))
-> ((1 == 1) && 2)
-> true
and $x
becomes 2 after it.在这个答案下的讨论之后我们可以看到最后的条件($x == 1 && $x = 2)
像(($x == 1) && ($x = 2))
-> ((1 == 1) && 2)
-> true
之后$x
变为 2。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.