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
https://forums.phpfreaks.com/topic/206409-code-executed-when-it-shouldnt-be/
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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.