【发布时间】:2025-11-22 12:30:01
【问题描述】:
我最近开始使用 Zend Studio,它报告了以下类型的代码警告:
$q = query("select * from some_table where some_condition");
while ($f = fetch($q)) {
// some inner workings
}
要停止警告,代码需要这样编写:
$q = query("select * from some_table where some_condition");
$f = fetch($q);
while ($f) {
// some inner workings
$f = fetch($q);
}
为什么这被标记为警告?有这么严重吗?
我了解该警告可能旨在阻止以下错误:
$a = 1;
while ($a = 1) {
// some inner workings
$a++;
}
它永远不会终止,因为 1 被分配给 $a,而 $a 又将 1 返回给 while 语句,而不是针对 $a 进行测试并在 $a 不为 1 时返回 false 给 while 语句。
容易犯的错误可能会验证警告,但在第二个示例中忘记在 while 块的末尾添加额外的 $f = fetch($q) 也会导致循环永远不会终止。如果我更改代码以删除警告,然后忘记在 while 块的末尾添加 $f = fetch($q) Zend 将不会警告!
因此,通过删除有关常见错误的警告,我将自己设置为另一个常见错误。
出锅,入火。
【问题讨论】:
标签: zend-framework warnings while-loop variable-assignment conditional-statements