php 为什么在条件不好的情况下分配?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/317259/
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
why are assignments in conditions bad?
提问by William Macdonald
I am using NetBeans for PHP 6.5.
我将 NetBeans 用于 PHP 6.5。
In my code I frequently use the following type of command:
在我的代码中,我经常使用以下类型的命令:
if (($row = $db->get_row($sql))) {
return $row->folder;
} else {
return FALSE;
}
Netbeans tells me that I should not be using assignments in the IF statement.
Netbeans 告诉我我不应该在 IF 语句中使用赋值。
Why ?
为什么 ?
回答by Toon Krijthe
They are not bad, but they can lead to dangerous mistakes.
它们并不坏,但它们可能导致危险的错误。
In c like languages, where an assignment is an expression, (to support for example a=b=c=1;) a common error is:
在类似 c 的语言中,赋值是一个表达式,(例如支持 a=b=c=1;)一个常见的错误是:
if (a = 1) { .. }
But you wanted to have
但你想拥有
if (a == 1) { .. }
Some developers have learned to type
一些开发人员已经学会了打字
if (1 == a) { .. }
To create an error if one '=' is forgotten. But I think that it does not improve the readability.
如果忘记了一个“=”,则会产生错误。但我认为它并没有提高可读性。
However modern compilers, give a warning if you write
然而,现代编译器,如果你写了一个警告
if (a = 1) { .. }
which I think is a better solution. In that case you are forced to check if it was what you really meant.
我认为这是一个更好的解决方案。在这种情况下,您将被迫检查这是否是您真正的意思。
回答by Tom Ritter
It's probably trying to help you avoid the dreaded typo:
它可能是为了帮助您避免可怕的拼写错误:
if(a = b) //logic error
if(a = b) //logic error
Although I would expect an enviroment smart enough to warn you about that, to also be smart enough to have "oh, don't worry about that case" conditions.
虽然我希望环境足够聪明来警告你这一点,但也足够聪明以拥有“哦,不要担心那种情况”的情况。
回答by rev
Conditionals often include short circuit operators. So, given this example:
条件通常包括短路运算符。所以,给定这个例子:
if ( a=func(x) && b=func(y) )
{
// do this
}
It may not be immediately obvious, but the second assignment would only occur if the first returned >0, and if func(y)had other side effects that you were expecting, they would not happen either.
这可能不是很明显,但第二个分配只会在第一个返回时发生>0,并且如果func(y)有您期望的其他副作用,它们也不会发生。
In short, if you know what you are doing and understand the side effects, then there is nothing wrong with it. However, you must consider the possibility that someone else may be maintaining your code when you're gone and they might not be as experienced as you.
简而言之,如果您知道自己在做什么并了解副作用,那么它就没有错。但是,您必须考虑在您离开时其他人可能会维护您的代码的可能性,而他们可能没有您那么有经验。
Also, future maintainers may think you intended the following:
此外,未来的维护者可能认为您打算执行以下操作:
if ( a==func(x) && b==func(y) ) ...
If they "fix" your code, they actually break it.
如果他们“修复”了您的代码,他们实际上会破坏它。
回答by Stein G. Strindhaug
In languages that allways return a value on assignments it's not bad(I think it's quite common in functional languages), but (as others allready have said while I typed this) it should usually be avoided since you or someone else might mistake it for a comparison. The compiler should usually warn about it, but it can be ignored if you're sure what you're doing...
在总是在赋值时返回值的语言中,这还不错(我认为这在函数式语言中很常见),但是(正如其他人在我输入时已经说过的那样)通常应该避免它,因为您或其他人可能会将其误认为是比较。编译器通常应该对此发出警告,但如果您确定自己在做什么,则可以忽略它...
回答by Stein G. Strindhaug
how would a code look like if you do not assign the $row value in the loop condition this would be much more complicated i think... although not that good to read for some maintainers, no? well you can do it like
如果您不在循环条件中分配 $row 值,代码会是什么样子,我认为这会复杂得多……虽然对某些维护者来说不太好阅读,不是吗?好吧,你可以这样做
$next = mysql_fetch_assoc($result)
do{
...
...
...
$next = mysql_fetch_assoc($result) or break;
}while ($next)
回答by Tim
I use them all the time, with loops (not sure why that would make a difference), like:
我一直使用它们,带有循环(不知道为什么会有所不同),例如:
$counter = 0;
while( $getWhateverDataObj = mysql_fetch_object( $sqlResult )) {
$getWhateverObj->firstName[$counter] = $getWhateverDataObj->firstName;
$getWhateverObj->lastName[$counter] = $getWhateverDataObj->lastName;
$counter++;
}
And it works fine.
它工作正常。

