jimfog Posted October 3, 2012 Share Posted October 3, 2012 In Netbeans, whenever I put assignments in conditionals I get the following warning message: Possible accidental assignment, assignments in conditions should be avoided. I am talking about conditional such as this for example: if ($result = $conn->query($query)) It could be other conditionals also(as the one above) where Netbeans will produce the warning message that I describe above. Why Netbeans might produce a warning in such cases-I do not see anything wrong with assignments in conditionals? Link to comment Share on other sites More sharing options...
php_developer Posted October 3, 2012 Share Posted October 3, 2012 single equal to sign is an assignment operator.. if you want that your if() condition should check for equality then use double equal to sign as if($result = = $conn->query($query)). Link to comment Share on other sites More sharing options...
Don E Posted October 3, 2012 Share Posted October 3, 2012 (edited) Not sure why you're getting the warning message but from my understanding that is legal.. Have you tried surrounding the above in parenthesis and see if you get the warning message? if ( ($result = $conn->query($query)) ) Or try: if (($result = $conn->query($query)) != false) Edited October 3, 2012 by Don E Link to comment Share on other sites More sharing options...
dsonesuk Posted October 3, 2012 Share Posted October 3, 2012 you are just assigning the $conn->query($query)) to $result, it is not checking if a certain condition exists. <?php$result = "hello";$result2 = "goodbye";if($result == $result2){echo '<p>(1)set $result value matches set $result2 value and passes this if condition<br />';echo '$result = "'.$result.'" : $result2 = "'.$result2.'"</p>';}else{echo '<p>(2)$result value does not match $result2 value<br />';echo '$result = "'.$result.'" : $result2 = "'.$result2.'"</p>';}if($result = $result2){echo '<p>(3)$result does not equal "hello" anymore but now equals "goodbye"<br />';echo '$result = "'.$result.'" : $result2 = "'.$result2.'"</p>';}if($result == $result2){echo '<p>(4)because $result now equals "goodbye" matches $result2 value of "goodbye" it now passes this if condition<br />';echo '$result = "'.$result.'" : $result2 = "'.$result2.'"</p>';}?> you would check with if condition, usually similar to this $result = $conn->query($query))if($result){//blah blah} Link to comment Share on other sites More sharing options...
Ingolme Posted October 3, 2012 Share Posted October 3, 2012 There is no problem with assignments in conditions. Netbeans is just warning you in case you did it accidentally. (A lot of threads on this forum would be avoided if more editors did that) Link to comment Share on other sites More sharing options...
boen_robot Posted October 3, 2012 Share Posted October 3, 2012 If you want a way to avoid this warning while also having the same effect, you can prefix the condition with "false !==". This will make NetBeans realize that while you're having an assignment, it's not an accidental one, since you're having an actual comparison with a higher priority.That said, in most cases, including this one, this is completely redundant, and would in fact slow down the script ever so slightly. You can safely ignore this warning if you really intend to assign a new value to the variable at each iteration/condition. Link to comment Share on other sites More sharing options...
jimfog Posted October 17, 2012 Author Share Posted October 17, 2012 There is a Netbeans feature where you can disable such warnings. My question was if there was anything wrong with this kind of code anyway. I will just disable the warnings and continue. Link to comment Share on other sites More sharing options...
boen_robot Posted October 20, 2012 Share Posted October 20, 2012 When you know what you're doing - no... as in "there's nothing wrong with such code".The warning is for newbies who do assignments when what they meant was an equals check. Heck, back when I was first learning PHP, I did this mistake too. Only instead of having NetBeans help me (I used Dreamwaver, which doesn't have such warnings), it was justsomeguy who did. Link to comment Share on other sites More sharing options...
jimfog Posted October 20, 2012 Author Share Posted October 20, 2012 things are now...crystal clear Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now