Jump to content

Archived

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

AV1611

Dumb Question

Recommended Posts

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' )){}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[!--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]

Share this post


Link to post
Share on other sites
[!--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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

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.