Zend Studio报告警告:分配情况。这样不好吗 [英] Zend Studio reports warning: Assignment in condition. Is this so bad?

查看:62
本文介绍了Zend Studio报告警告:分配情况。这样不好吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始使用Zend Studio,它已警告以下类型的代码:

I have recently started using Zend Studio which has reported as warning the following type of code:

$q = query("select * from some_table where some_condition");
while ($f = fetch($q)) {
  // some inner workings
}

要停止警告,需要这样编写代码:

To stop the warning the code needs to be written like this:

$q = query("select * from some_table where some_condition");
$f = fetch($q);
while ($f) {
  // some inner workings
  $f = fetch($q);
}

为什么将此标记为警告?

Why is this marked as a warning? Is it so bad?

我了解该警告可能旨在阻止此类错误:

I understand that the warning may be designed to stop errors like this:

$a = 1;
while ($a = 1) {
  // some inner workings
  $a++;
}

永远不会终止,因为将1分配给$ a会返回while语句为1,而不是针对$ a进行测试,并且在$ a不为1的情况下向while语句返回false。

which will never terminate because 1 is being assigned to $a which in turn returns 1 to the while statement, rather than being tested against $a and returning false to the while statement when $a is not 1.

容易出错,可以验证a警告,虽然可以,但是却忘记了在第二个示例的while块的末尾添加额外的$ f = fetch($ q),这也将导致循环永远不会终止。如果我更改代码以删除警告,然后忘记在while块的末尾添加$ f = fetch($ q),则Zend不会发出警告!

Easy error to make which may validate a warning, granted, but so is forgetting to add the extra $f = fetch($q) at the end of the while block in the second example which will also result in a loop that will never terminate. If I change my code to remove the warning and then forget to add the $f = fetch($q) at the end of the while block Zend won't warning be about that!

因此,通过删除有关常见错误的警告,我将自己设置为其他常见错误。

So by removing the warning concerning a common error I'm setting myself up for a different common error.

出锅,入火。

推荐答案

while (($row = $sql->db_Fetch("MYSQL_ASSOC")) != false)

这篇关于Zend Studio报告警告:分配情况。这样不好吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆