Dynadot โ€” .com Transfer

Php if - Is this possible?

SpaceshipSpaceship
Watch

PoorDoggie

Soon to be RICHdoggie!VIP Member
Impact
18
is it possible to do something like this in php?

PHP:
if($var == "this" OR "that" OR "something else"){

}

instead of having to do it like this:

PHP:
if($var == "this" || $var == "that" || $var == "something else"){

}

Thanks
Tom
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
no
 
0
•••
Is there any particular reason you want to do that, anyway?

I mean, what's so hard about learning to write in the language as it is?
 
0
•••
you can make use of the switch function :)

like

PHP:
switch($var){
	case 'this':
	case 'that':
	case 'something else':
		do_that();
	break;
....
}

HTH :)
 
0
•••
i just really couln't be bothered to write it out full, and if there was a shorter way I would rather have used that. Also, I can't make use of the switch thing because it needs to be if $var == any of these values then do this, otherwise dont. switch would just take too long.

Thanks anyhow
Tom
 
0
•••
no, but why not just make a function out of it.

Code:
function or($var,$var1,$var2,$var3) {
    if($var == "$var2" || $var == "$var2" || $var == "$var3") return true;
    else return false;
}

if (or($var1,"blah","blah","blah")) {
} else { }

That way you could use it over and over again and not have to write so much?
 
0
•••
There is switch and if you don't want to use switch then use nested IF's
 
0
•••
PHP:
if(in_array($var, array("this", "that", "something else"))) {
 
1
•••
bliss said:
no, but why not just make a function out of it.

Code:
function or($var,$var1,$var2,$var3) {
    if($var == "$var2" || $var == "$var2" || $var == "$var3") return true;
    else return false;
}

if (or($var1,"blah","blah","blah")) {
} else { }

That way you could use it over and over again and not have to write so much?
lol ... its not that much of a problem, although that function is a v. good idea to save time :D thanks
nick_mayhem said:
There is switch and if you don't want to use switch then use nested IF's
what are nested ifs?
 
0
•••
Nice one Scott.


Nested if's
Code:
if($var == "something") {
  if($var == "blah") {
    if($var == "something else") {
    }
  }
} else { }

Nested if's could be used as a fucntion to with a for statement too. :D

Code:
function or($var) {
   for($c=1;$c>count($c);++$c) {
        if($var[0] != $var[$c]) { return false }
   }
   return true;
}

if(or(array($v,"sweet","shivy","something else"))) { echo "sweet"; }
else { echo "dude"; }

Not to sure i got that right, kind of tired. The general idea is there that you don't have to worry about how many your comparing, 1 to X number of "something" with the orginal $var.

-
 
Last edited:
0
•••
PHP:
if($var == "this")
{
if($var == "that")
{
if($var == "something else")
{
  Statements here.
}
}
}
^^^ Nested If's
 
0
•••
Nested ifs wouldn't solve his original problem of having to write out "|| $var == 'foo'" each time. Nested ifs work more like && operators than ||s.
 
0
•••
lol.. thanks for that. I never knew that was called nested ifs! and tree is right, it dosen't solve it. Its not that big a deal, lol, although thanks for all your help. I was really just wondering if php had the capabilities built in to do something like that, rather than having to write it out in full. Just to save time, lol :D

Anyhow, thanks a lot everyone. Your scripts and solutions have been most interesting, and in fact some have helped me in other ways! :D

Thanks
Tom
 
0
•••
nested ifs are bad. go with scotts method.
 
0
•••
If you write your code in an organized way, nested ifs are fine.
 
0
•••
i find nested ifs quite useful sometimes becuase it lays the code out in an organised and logical way, rather than having huge if() statements.

Anyhow. Thanks for all your help! :D
 
0
•••
I always go with nested IF's even if it is more then 25 IF's

Switch is also good. But sometimes I get confused in it. And IF's are coming out in some kind of rhythym. So it does all the work without thinking how and hey, what to write for the switch.
 
0
•••
I misread the original question
Scott said:
PHP:
if(in_array($var, array("this", "that", "something else"))) {

Is a good solution

Last week I used a nested switches for the first time in a long time. As long as you comment everything and tab the code properly it becomes less confusing ;p
 
0
•••
nick_mayhem said:
I always go with nested IF's even if it is more then 25 IF's

Switch is also good. But sometimes I get confused in it. And IF's are coming out in some kind of rhythym. So it does all the work without thinking how and hey, what to write for the switch.
I will usually go for long complicated ifs like here is one I was doing yesterday:
PHP:
if($_GET['month'] && is_numeric($_GET['month']) && ($_GET['month'] == 1 || $_GET['month'] == 2 || $_GET['month'] == 3 || $_GET['month'] == 4)){

}
mainly because if you want an "else" bit, with nested ifs there will be loads of the same else bits, and if that else bit is a long piece of html or something then it just gets too complicated. And I hate using includes! lol
 
0
•••
Spaceship
Domain Recover
CatchDoms
DomainEasy โ€” Payment Flexibility
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back