Zend Studio reports warning: Assignment in conditi

2019-01-19 14:06发布

问题:

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.

回答1:

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


回答2:

So you won't have to rewrite all your code without a decent reason: You can disable the detection of this potential programming error in Window | Preferences, PHP | Semantic Analysis.



回答3:

This is probably marked as a warning because people often use "=" by mistake when they mean "==".

eg:

$a = 1
while($a = 1) {
   $a++;
}

This will never terminate, though if you thought you'd written "==", it should.



回答4:

Zend Studio is trying to help you in writing better code that debugs easier. Disabling Semantic checking is not a good idea, it just sweeps potential problems under the carpet and you'll be missing out on the real problems. That IS a decent reason! Don't avoid warning messages by ignoring them, modify your code by implementing the correct solution.



回答5:

As you well know Zend Studio is built on eclipse, which is a Java IDE. In the Java language it's ilegal to do something like this:

String s;
while (s = getName()) {
    ...
}

That's because even if 'getName' returns a null value it will be assigned to 's' and casting between objects and booleans (which is the required type by the condition statements) is a bit more subjective as with PHP, therefore it will throw an exception in compile time.

The situation in PHP may be different, but for some reason the Zend developers decided to leave this warning active by default, you can disable it as mentioned before but i consider it will help you when a real assignment in condition happens.

Cleaning the warning is quite easy by just assigning the results and then comparing it like this:

if (($result = $mysqli->query ( $query )) == true) {

Instead of:

if ($result = $mysqli->query ( $query )) {

As you can see you don't need additional bunches of code.

Anyway it is just a warning, you don't have to worry of them much.



回答6:

Actually, I guess your question has already been answered, somewhat. But to address your actual problem, I think this might help.

//i dont know what is returned if there are no more records to fetch...
//but lets assume it is a boolean value
while (($f = fetch($q))!= false)
{
    $this->doSomethingVeryImportantThatMakesYourBossHappy($f);
}

That should do the trick and the "Assignment in condition"-message should disappear.

As a sidenote: use the equals operator the same way as when you negate stuff. You also use the equals sign with other operators like

if ($falseness != false){$trueness = true}

and not

if ($falseness ! false){$trueness = false}

That helps me to always remember how to compare values and not assign values to them.



回答7:

The reason it's bad is alot of people use "=" when then meant "=="

The = operator will return the assigment to the left so if you use if($x=true) the code within the if will be run, if you use if($x=false) the code will not be run. It's a neat trick that can save a line or two of code but it's also dangerous because if you meant if($x == false) and typed if($x = false) it will be a bug that can be difficult to track down.



回答8:

Nay my friends all assignments in the condition generate this warning. I do not want to turn this off completely, as = instead of == is a syntax error i am prone to. As for question of why it is necessary, I will use an example from the PHP Manual. This is from the section on the "MySQL improved" extensions or mysqli:

$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5";

if ($result = $mysqli->query($query)) {

    /* fetch associative array */
    while ($row = $result->fetch_assoc()) {
        printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);
    }

unfortunately, I have developed my database functions using this technique and am trying to use them in Zend Studio. This error is popping up enough times to be a real pain. I will rephrase with the suggestions here, as I value unambiguous code, however I am also going to pop over to the PHP manual and suggest they change the example to use the better style. Perhaps some of you could do the same and we could improve the documentation?!