Jump to content

code executed when it shouldn't be


siwelis

Recommended Posts

For some reason I have some code like this that executes even when the variable isn't set. Is my syntax wrong?

 

$setting = mysql_real_escape_string($_POST['setting']);
//value posted for setting is "yes"

if($setting = "correct"){
echo Hello;
}

//echo's Hello
//works right



////OTHER SCENARIO



$setting = mysql_real_escape_string($_POST['setting']);
//value is not posted for setting

if($set = "yes"){
echo Hello;
}

//Still echo's Hello

 

I know I could just do a check to see if the variable is set and if not exit() or similar, but I really shouldn't have to... right? I don't mind workarounds, but I'd rather work it through.

 

Link to comment
Share on other sites

Yes, the syntax is wrong. You're performing an assignment, rather than a comparison.

 

if( $setting = 'correct' ) has no problem assigning 'corect' to setting, therefore always evaluates as true.

 

if( $setting == 'correct' ) will compare the values.

Link to comment
Share on other sites

if( $setting = 'correct' ) has no problem assigning 'corect' to setting, therefore always evaluates as true.

 

LOL! I'm glad PHP has no problem with that! - Thanks a lot guys! This clears up a LOT of issues I've had.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.