Jump to content

assignments in conditions


jimfog

Recommended Posts

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

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 by Don E
Link to comment
Share on other sites

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

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

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

  • 2 weeks later...

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

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

things are now...crystal clear

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...