Jump to content


Photo

Dumb Question


  • Please log in to reply
4 replies to this topic

#1 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 21 June 2006 - 05:44 PM

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

#2 Buyocat

Buyocat
  • Members
  • PipPipPip
  • Advanced Member
  • 267 posts

Posted 21 June 2006 - 05:53 PM

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.
Looking for some easy-to-use tools?  Try these, https://sourceforge....jects/utils-php -- I made them myself.  They're distinct tools which are easy to understand and use.  See some examples uses at http://www.anotherearlymorning.com

#3 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 21 June 2006 - 06:29 PM

[!--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:
$variable = (!isset($variable) or $variable == 'string') ? "A" : "B";


#4 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 21 June 2006 - 06:50 PM

[!--quoteo(post=386524:date=Jun 22 2006, 04:29 AM:name=wildteen88)--][div class=\'quotetop\']QUOTE(wildteen88 @ Jun 22 2006, 04:29 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
close, it should be this:
$variable = (!isset($variable) or $variable == 'string') ? "A" : "B";
[/quote]
Close, it should be this.
$variable = (!isset($variable) or $variable != 'string') ? "A" : "B";


#5 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 21 June 2006 - 07:07 PM

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:
if ($variable != "ABC") {
  echo "error";
}
does the exact same thing as this:
if (!isset($variable) or $variable != "ABC") {
  echo "error";
}
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:
if (!isset($variable)) {
   $error_message = 'Variable is not set';
} elseif ($variable != "ABC") {
   $error_message = "Variable does not equal ABC";
}
echo $error_message;

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.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users