“条件分配”背后的基本原理是什么? Zend Studio IDE 中出现警告?

发布于 2024-10-04 00:56:05 字数 230 浏览 1 评论 0原文

给定:

if ($variable = get_variable('variable')) {
    // ...
}

*$variable = get_variable('variable')* 在 Zend Studio 中抛出“Assignment in condition”警告。我明白这个警告的意思,但有人知道它背后的理由是什么吗?仅仅是编码约定、可读性等问题吗?

Given:

if ($variable = get_variable('variable')) {
    // ...
}

The *$variable = get_variable('variable')* throws an 'Assignment in condition' warning in Zend Studio. I understand what the warning means, but does anyone know what the rationale behind it is? Is it merely coding conventions, a matter of readability, etc.?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

潜移默化 2024-10-11 00:56:05

这是大多数允许这种构造的语言中的 IDE/编译器发出的一个非常常见的警告:因为 = (赋值)和 == (比较)非常相似,并且比较在 if 语句中更常见,警告只是让您知道您可能在真正想要进行比较的地方错误地进行了赋值。

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.

姐不稀罕 2024-10-11 00:56:05

这样做是因为:

if ($variable = get_variable('variable')) {
    // ...
}

非常接近:

if ($variable == get_variable('variable')) {
    // ...
}

前者并不是一个好的实践。 Zend Studio 假设您更有可能指的是后一种情况,因此它会对此发出警告。并不是说这不是一个有用的工具。在 while 循环中通常更容易接受,用于逐行读取文件(当仍有一行要读取时)。问题是很难快速挑选出来。

It does this because:

if ($variable = get_variable('variable')) {
    // ...
}

is very close to:

if ($variable == get_variable('variable')) {
    // ...
}

The former is not exactly a good practice to get into. Zend Studio assumes that you are more likely to have meant the latter case, so it warns you about this. Not to say that this isn't a useful tool. It is usually more acceptable in a while loop, for reading a file line by line (while there is still a line to read). The problem is that it is hard to quickly pick out.

蓝礼 2024-10-11 00:56:05

我相信这主要是因为人们通常会忘记双等号。这应该消除警告:

if ($variable = get_variable('variable') != false) {
    // ...
}

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) {
    // ...
}
掩于岁月 2024-10-11 00:56:05

因为通常这只是一个拼写错误,如果你忘记了一个“=”

if ($a = $b) { /* $a and $b equal? */ }

,那么 IDE 建议你看一下它。

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.

半衾梦 2024-10-11 00:56:05

编写赋值运算符 = 而不是相等检查 == 是非常常见的错误。

在所有情况下,我知道您可以通过将作业括在括号中来消除该警告,如下所示。

if (($var = 1))
{
    /* ... */
}

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))
{
    /* ... */
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文