Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend Studio reports warning: Assignment in condition. Is this so bad?

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++;
}

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.

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.

Out of the pan, into the fire.

like image 626
Stacey Richards Avatar asked Mar 10 '09 12:03

Stacey Richards


1 Answers

while (($row = $sql->db_Fetch("MYSQL_ASSOC")) != false)
like image 143
Iurii Avatar answered Nov 03 '22 10:11

Iurii