[advanced search]
 

Go Back   NamePros.com > Discussion > Web Design & Development > Programming

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.


Closed Thread
 
LinkBack Thread Tools
Old 08-27-2006, 06:11 AM   #1 (permalink)
Soon to be RICHdoggie!
 
PoorDoggie's Avatar
 
Join Date: Jan 2005
Location: UK
Posts: 2,390
316.50 NP$ (Donate)

PoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nice


php if - is this possible?

is it possible to do something like this in php?

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

}
instead of having to do it like this:

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

}
Thanks
Tom
PoorDoggie is offline  
Old 08-27-2006, 06:54 AM   #2 (permalink)
NamePros Regular
 
Noobie's Avatar
 
Join Date: Feb 2006
Location: Montreal, Quebec, Canada
Posts: 324
66.75 NP$ (Donate)

Noobie is on a distinguished road


no
__________________
Goldkey.com is a scam
What's your BMI? | Timestamp Generator
Noobie is offline  
Old 08-27-2006, 07:03 AM   #3 (permalink)
NamePros Member
 
Join Date: May 2006
Posts: 160
81.00 NP$ (Donate)

TwistMyArm is on a distinguished road


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?
TwistMyArm is offline  
Old 08-27-2006, 07:09 AM   #4 (permalink)
Senior Member
 
ahtum's Avatar
 
Join Date: May 2006
Posts: 1,266
1,620.58 NP$ (Donate)

ahtum has much to be proud ofahtum has much to be proud ofahtum has much to be proud ofahtum has much to be proud ofahtum has much to be proud ofahtum has much to be proud ofahtum has much to be proud ofahtum has much to be proud ofahtum has much to be proud ofahtum has much to be proud of

Ethan Allen Fund Find Marrow Donors!
you can make use of the switch function

like

PHP Code:
switch($var){
    case
'this':
    case
'that':
    case
'something else':
        
do_that();
    break;
....
}
HTH
ahtum is offline  
Old 08-27-2006, 07:11 AM   #5 (permalink)
Soon to be RICHdoggie!
 
PoorDoggie's Avatar
 
Join Date: Jan 2005
Location: UK
Posts: 2,390
316.50 NP$ (Donate)

PoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nice


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
PoorDoggie is offline  
Old 08-27-2006, 07:22 AM   #6 (permalink)
NamePros Regular
 
bliss's Avatar
 
Join Date: Dec 2005
Posts: 952
176.95 NP$ (Donate)

bliss is a name known to allbliss is a name known to allbliss is a name known to allbliss is a name known to allbliss is a name known to allbliss is a name known to all


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?
bliss is offline  
Old 08-27-2006, 07:23 AM   #7 (permalink)
I'll do it

Technical Services

 
-Nick-'s Avatar
 
Join Date: Dec 2005
Location: India
Posts: 6,434
5,169.80 NP$ (Donate)

-Nick- has a reputation beyond repute-Nick- has a reputation beyond repute-Nick- has a reputation beyond repute-Nick- has a reputation beyond repute-Nick- has a reputation beyond repute-Nick- has a reputation beyond repute-Nick- has a reputation beyond repute-Nick- has a reputation beyond repute-Nick- has a reputation beyond repute-Nick- has a reputation beyond repute-Nick- has a reputation beyond repute

Adoption
There is switch and if you don't want to use switch then use nested IF's
-Nick- is offline  
Old 08-27-2006, 07:25 AM   #8 (permalink)
Senior Member
 
Scott's Avatar
 
Join Date: Jun 2003
Location: UK
Posts: 3,773
58.82 NP$ (Donate)

Scott has a reputation beyond reputeScott has a reputation beyond reputeScott has a reputation beyond reputeScott has a reputation beyond reputeScott has a reputation beyond reputeScott has a reputation beyond reputeScott has a reputation beyond reputeScott has a reputation beyond reputeScott has a reputation beyond reputeScott has a reputation beyond reputeScott has a reputation beyond repute

Save The Children Save The Children Save The Children Save The Children Save The Children
PHP Code:
if(in_array($var, array("this", "that", "something else"))) {
Scott is offline  
Old 08-27-2006, 07:25 AM   #9 (permalink)
Soon to be RICHdoggie!
 
PoorDoggie's Avatar
 
Join Date: Jan 2005
Location: UK
Posts: 2,390
316.50 NP$ (Donate)

PoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nice


Quote:
Originally Posted by bliss
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 thanks
Quote:
Originally Posted by nick_mayhem
There is switch and if you don't want to use switch then use nested IF's
what are nested ifs?
PoorDoggie is offline  
Old 08-27-2006, 07:28 AM   #10 (permalink)
NamePros Regular
 
bliss's Avatar
 
Join Date: Dec 2005
Posts: 952
176.95 NP$ (Donate)

bliss is a name known to allbliss is a name known to allbliss is a name known to allbliss is a name known to allbliss is a name known to allbliss is a name known to all


Help!

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.

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 by bliss; 08-27-2006 at 07:36 AM.
bliss is offline  
Old 08-27-2006, 08:29 AM   #11 (permalink)
I'll do it

Technical Services

 
-Nick-'s Avatar
 
Join Date: Dec 2005
Location: India
Posts: 6,434
5,169.80 NP$ (Donate)

-Nick- has a reputation beyond repute-Nick- has a reputation beyond repute-Nick- has a reputation beyond repute-Nick- has a reputation beyond repute-Nick- has a reputation beyond repute-Nick- has a reputation beyond repute-Nick- has a reputation beyond repute-Nick- has a reputation beyond repute-Nick- has a reputation beyond repute-Nick- has a reputation beyond repute-Nick- has a reputation beyond repute

Adoption
PHP Code:
if($var == "this")
{
if(
$var == "that")
{
if(
$var == "something else")
{
  
Statements here.
}
}
}
^^^ Nested If's
-Nick- is offline  
Old 08-27-2006, 04:45 PM   #12 (permalink)
NamePros Regular
 
Tree's Avatar
 
Join Date: Feb 2006
Location: Atlanta, GA, USA
Posts: 335
13.25 NP$ (Donate)

Tree will become famous soon enoughTree will become famous soon enough


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.
Tree is offline  
Old 08-28-2006, 01:25 AM   #13 (permalink)
Soon to be RICHdoggie!
 
PoorDoggie's Avatar
 
Join Date: Jan 2005
Location: UK
Posts: 2,390
316.50 NP$ (Donate)

PoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nice


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

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

Thanks
Tom
PoorDoggie is offline  
Old 08-28-2006, 02:29 AM   #14 (permalink)
Senior Member
 
Join Date: May 2003
Posts: 2,211
6,170.25 NP$ (Donate)

adam_uk is a jewel in the roughadam_uk is a jewel in the roughadam_uk is a jewel in the rough

Breast Cancer
nested ifs are bad. go with scotts method.
adam_uk is offline  
Old 08-28-2006, 02:47 AM   #15 (permalink)
NamePros Regular
 
Tree's Avatar
 
Join Date: Feb 2006
Location: Atlanta, GA, USA
Posts: 335
13.25 NP$ (Donate)

Tree will become famous soon enoughTree will become famous soon enough


If you write your code in an organized way, nested ifs are fine.
Tree is offline  
Old 08-28-2006, 06:36 AM   #16 (permalink)
Soon to be RICHdoggie!
 
PoorDoggie's Avatar
 
Join Date: Jan 2005
Location: UK
Posts: 2,390
316.50 NP$ (Donate)

PoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nice


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!
PoorDoggie is offline  
Old 08-28-2006, 07:26 AM   #17 (permalink)
I'll do it

Technical Services

 
-Nick-'s Avatar
 
Join Date: Dec 2005
Location: India
Posts: 6,434
5,169.80 NP$ (Donate)

-Nick- has a reputation beyond repute-Nick- has a reputation beyond repute-Nick- has a reputation beyond repute-Nick- has a reputation beyond repute-Nick- has a reputation beyond repute-Nick- has a reputation beyond repute-Nick- has a reputation beyond repute-Nick- has a reputation beyond repute-Nick- has a reputation beyond repute-Nick- has a reputation beyond repute-Nick- has a reputation beyond repute

Adoption
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.
-Nick- is offline  
Old 08-28-2006, 07:33 AM   #18 (permalink)
NamePros Regular
 
Noobie's Avatar
 
Join Date: Feb 2006
Location: Montreal, Quebec, Canada
Posts: 324
66.75 NP$ (Donate)

Noobie is on a distinguished road


I misread the original question
Quote:
Originally Posted by Scott
PHP Code:
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
__________________
Goldkey.com is a scam
What's your BMI? | Timestamp Generator
Noobie is offline  
Old 08-29-2006, 04:29 AM   #19 (permalink)
Soon to be RICHdoggie!
 
PoorDoggie's Avatar
 
Join Date: Jan 2005
Location: UK
Posts: 2,390
316.50 NP$ (Donate)

PoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nice


Quote:
Originally Posted by nick_mayhem
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 Code:
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
PoorDoggie is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Site Sponsors
Advertise your business at NamePros

All times are GMT -7. The time now is 12:39 AM.


Powered by: vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0
Template-Modifications by TMS
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85