Jump to content
jimfog

assignments in conditions

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?

Share this post


Link to post
Share on other sites

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)).

Share this post


Link to post
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

Share this post


Link to post
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}

Share this post


Link to post
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)

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

things are now...crystal clear

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...