...

View Full Version : Abbreviated "If" Statement



Hawkmoon
10-06-2004, 10:47 PM
Is there a short form of:
if($var=="this"||$var=="that")

Such as:
if($var=="this"||"that")

Dylan Leblanc
10-07-2004, 12:43 AM
No, nothing shorter that that first one. That's tthe way boolean loggic works.

This is what I do if I have to test for more than one value for a single variable. You can add as many elements to the array as you please.

if (in_array($var, array('this', 'that')))

Fou-Lu
10-07-2004, 12:45 AM
Sorry, none that I am aware of. Of course, there are easier ways if you are using mulitple criteria, and longer ways as well. But what you have is pretty much the easiest. I'd recommend single quoting though, and not using pipebars, because if you have numbers and forget the quotes, you'll be attempting to validate a bitwise value instead.



if ($var == 'this' OR $var == 'that')
{
//Do whatever.
}


Oops, a little late, but meh.

Hawkmoon
10-07-2004, 12:48 AM
Thanks Fou and Dylan.

ghaspias
10-07-2004, 01:00 AM
switch($var){
case "this";
case "that":{/*
your code here...
*/}
}

__or__

if(strpos("thisthat",$var)){...}

(Not shure if this works. It would match $var="isth", or $var="s", for instance.)

and as above

in_array($var,['this','that'])?/*your code here*/:/*else*/;

Spookster
10-07-2004, 01:14 AM
I'd recommend single quoting though, and not using pipebars, because if you have numbers and forget the quotes, you'll be attempting to validate a bitwise value instead.



How do you figure that? A bitwise OR uses one |. In this case they are going to be using ||. I don't see how quoting can cause that to do a bitwise operation.

Fou-Lu
10-07-2004, 01:17 AM
Oops, I suppose so eh? Maybe I'm just on crack!

Still, I'd use the or, the pipebars are kinda depreciated.

Hawkmoon
10-07-2004, 01:57 AM
FYI, Pipebars aren't depreciated. In fact they have a higher precedance than "OR":

http://us4.php.net/manual/en/language.operators.php#language.operators.precedence

Spookster
10-07-2004, 03:04 AM
Oops, I suppose so eh? Maybe I'm just on crack!

Still, I'd use the or, the pipebars are kinda depreciated.

Put your hands in the air and step away from the crack pipe. :D

Celtboy
10-08-2004, 02:00 AM
perhaps:



(($this == "that" ) || ($this == "this")) ? return true : return false;

Kurashu
10-08-2004, 02:45 AM
Ternaries are fun. =)

Fou-Lu
10-09-2004, 12:11 AM
FYI, Pipebars aren't depreciated. In fact they have a higher precedance than "OR":

http://us4.php.net/manual/en/language.operators.php#language.operators.precedence

Now I'm confused. I've always used OR / AND with any non integer variable because I thought they were a higher precedence. Hmm, question then, is there any practical use of pipebars taking precedence over the OR when it comes to the non-integer variables? Probably not, but I guess I should check to see if anyone has had problems in the past.

Hawkmoon
10-09-2004, 12:37 AM
Yeah...don't know that there is any real USEFUL application for the precedence...I guess potentially for the following:


if($var=="this" || $var=="that" OR $var2=="this") {
//If you wanted it to see if $var matched those values before you checked $var2
}

Kurashu
10-09-2004, 01:50 AM
That would make no sense though. You are checking to see IF $var is equal to this or that, or if $var2 is equal to that. Now, if you wanted to check something like $var1 is equal to this AND $var2 is equal to this or that, you would use something like this:



if ($var == "this" && ($var2 == "this" || $var2 == "that") { /*content */ }

Hawkmoon
10-09-2004, 05:46 AM
Kurashu -
The above example was to show the precedence of pipebars over "OR" and was not addressing my original question. See Spookster's and my comments to Fou-Lu about pipebars.

-Hawkmoon

Kurashu
10-09-2004, 12:00 PM
Ok, I see I what you mean now.

Fou-Lu
10-09-2004, 03:13 PM
Ok, so just to sum it up here...

I've never used both in any type of comparison before, however it can have its usefulness correct? Precidence with it would reflect kind of how mathmatical precidence works in say:
x= 2 + 4 * 4
Where it will calculate the 4x4 first correct?

So it would be kind of useless combining the || and the OR but the && and the OR, or the || and AND would combine together successfully?

Such as:
if ($var == 'this' || $var == 'that' AND !empty($somethingelse))
will validate if either $var is set and $somethingelse as well?
Kind of like:
if (($var == 'this' OR $var == 'that') AND !empty($somethinelse))

If I'm wrong, don't bother correcting cause it would probably take forever for a person such as me already set in my ways. If both of those functions would validate the same, that would make me happy, but either way my style would always be the latter, unless theres a reason as to why I shouldn't be doing it ;)

Hawkmoon
10-09-2004, 03:31 PM
Right on the money Fou-Lu! Good summary to end this now "Hot" thread :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum