鉴于:
if ($variable = get_variable('variable')) {
// ...
}
*$variable = get_variable('variable')* 在 Zend Studio 中引发“条件分配”警告。我理解警告的含义,但有人知道它背后的基本原理是什么吗?仅仅是编码约定、可读性等问题吗?
鉴于:
if ($variable = get_variable('variable')) {
// ...
}
*$variable = get_variable('variable')* 在 Zend Studio 中引发“条件分配”警告。我理解警告的含义,但有人知道它背后的基本原理是什么吗?仅仅是编码约定、可读性等问题吗?
This is a very common warning issued by IDEs/compilers in most languages that allow this construct: since =
(assignment) and ==
(comparison) are very similar, and comparison is more common within an if
statement, the warning is just there to let you know that you may have put in an assignment by mistake where you really intended a comparison.
它这样做是因为:
if ($variable = get_variable('variable')) {
// ...
}
非常接近:
if ($variable == get_variable('variable')) {
// ...
}
前者并不是一个很好的做法。Zend Studio 假定您更可能指的是后一种情况,因此它会就此向您发出警告。并不是说这不是一个有用的工具。通常在while
循环中更容易接受,用于逐行读取文件(同时还有一行要读取)。问题是很难快速挑选出来。
I believe it's mainly there because people normally forget the double equals. This should get rid of the warning:
if ($variable = get_variable('variable') != false) {
// ...
}
Because often its just a typo, if you forgot one "="
if ($a = $b) { /* $a and $b equal? */ }
So the IDE advise you to have a look at it.
It's very very common mistake to write assignment operator =
instead of equality check ==
.
In all cases I know you can get rid of that warning by wrapping the assignment in parenthesis like this.
if (($var = 1))
{
/* ... */
}