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

后端 未结 8 1629
轻奢々
轻奢々 2020-12-11 02:47

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_conditio         


        
相关标签:
8条回答
  • 2020-12-11 03:17

    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?!

    0 讨论(0)
  • 2020-12-11 03:18

    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.

    0 讨论(0)
  • 2020-12-11 03:23

    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.

    0 讨论(0)
  • 2020-12-11 03:31

    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.

    0 讨论(0)
  • 2020-12-11 03:35

    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.

    0 讨论(0)
  • while (($row = $sql->db_Fetch("MYSQL_ASSOC")) != false)
    
    0 讨论(0)
提交回复
热议问题