AV1611 Posted June 21, 2006 Share Posted June 21, 2006 I need a clause that checks to see if a variable is set, then checks the value of the variable...I suck at tenerary operators...if the variable $_post['pw'] is not set or if it is set but is not == 'ABC' then {}if(!isset($_post['pw'] ????? $_post['pw']!='ABC' )){} Quote Link to comment https://forums.phpfreaks.com/topic/12565-dumb-question/ Share on other sites More sharing options...
Buyocat Posted June 21, 2006 Share Posted June 21, 2006 This isn't a ternary operator, but what you want is:if (!isset($variable) or $variable == 'string'){// do stuff}If you want it to be a ternary then I believe it looks something like:$variable = ? (!isset($variable or $variable == 'string') "A" : "B";If the statement is evaluated as true then $variable will equal A else B. Quote Link to comment https://forums.phpfreaks.com/topic/12565-dumb-question/#findComment-48143 Share on other sites More sharing options...
wildteen88 Posted June 21, 2006 Share Posted June 21, 2006 [!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]If you want it to be a ternary then I believe it looks something like:$variable = ? (!isset($variable or $variable == 'string') "A" : "B";[/quote]close, it should be this:[code]$variable = (!isset($variable) or $variable == 'string') ? "A" : "B";[/code] Quote Link to comment https://forums.phpfreaks.com/topic/12565-dumb-question/#findComment-48161 Share on other sites More sharing options...
trq Posted June 21, 2006 Share Posted June 21, 2006 [!--quoteo(post=386524:date=Jun 22 2006, 04:29 AM:name=wildteen88)--][div class=\'quotetop\']QUOTE(wildteen88 @ Jun 22 2006, 04:29 AM) [snapback]386524[/snapback][/div][div class=\'quotemain\'][!--quotec--]close, it should be this:[code]$variable = (!isset($variable) or $variable == 'string') ? "A" : "B";[/code][/quote]Close, it should be this.[code]$variable = (!isset($variable) or $variable != 'string') ? "A" : "B";[/code] Quote Link to comment https://forums.phpfreaks.com/topic/12565-dumb-question/#findComment-48169 Share on other sites More sharing options...
.josh Posted June 21, 2006 Share Posted June 21, 2006 you do not need the extra isset condition in your if statement, if you are checking to see if it is != "ABC". checking if $variable != "ABC" already implies whether it is set or not. example:[code]if ($variable != "ABC") { echo "error";}[/code]does the exact same thing as this:[code]if (!isset($variable) or $variable != "ABC") { echo "error";}[/code]well, there is a slight difference in how the condition is processed. In reality, in this 2nd code block, the condition will return TRUE if the variable does not exist, and it will completely skip even checking to see whether if it is != "ABC". But in the first code block given, whether $variable = "XYZ" or whether $variable does not exist at all, if $variable does not equal "ABC" then the condition will return TRUE. in other words, if $variable does not exist, then it can't equal "ABC", so the error message will display. The only reason why you would want to break down checking a variable for certain things is if you want to do like, custom error messages. For instance, if you were to do this:[code]if (!isset($variable)) { $error_message = 'Variable is not set';} elseif ($variable != "ABC") { $error_message = "Variable does not equal ABC";}echo $error_message;[/code]but if you are just wanting to do a generic "there was an error" message, without being specific as to what the error was, simply doing the first code block will suffice. Quote Link to comment https://forums.phpfreaks.com/topic/12565-dumb-question/#findComment-48176 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.