NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page php if - is this possible?

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

Advanced Search


Closed Thread
 
LinkBack Thread Tools
Old 08-27-2006, 07:11 AM THREAD STARTER               #1 (permalink)
Soon to be RICHdoggie!
 
PoorDoggie's Avatar
Join Date: Jan 2005
Location: UK
Posts: 2,408
PoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie 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"){
????: NamePros.com http://www.namepros.com/programming/231959-php-if-is-this-possible.html


Thanks
Tom
PoorDoggie is offline  
Old 08-27-2006, 07:54 AM   #2 (permalink)
NamePros Regular
 
Noobie's Avatar
Join Date: Feb 2006
Location: Montreal, Quebec, Canada
Posts: 324
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, 08:03 AM   #3 (permalink)
NamePros Member
Join Date: May 2006
Posts: 160
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, 08:09 AM   #4 (permalink)
Senior Member
 
ahtum's Avatar
Join Date: May 2006
Posts: 1,330
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':
????: NamePros.com http://www.namepros.com/showthread.php?t=231959
    case 
'that':
    case 
'something else':
        
do_that();
    break;
....

HTH
ahtum is offline  
Old 08-27-2006, 08:11 AM THREAD STARTER               #5 (permalink)
Soon to be RICHdoggie!
 
PoorDoggie's Avatar
Join Date: Jan 2005
Location: UK
Posts: 2,408
PoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie 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, 08:22 AM   #6 (permalink)
NamePros Regular
 
bliss's Avatar
Join Date: Dec 2005
Posts: 951
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 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, 08:23 AM   #7 (permalink)
I'll do it
 
-Nick-'s Avatar
Join Date: Dec 2005
Location: India
Posts: 6,927
-Nick- Has achieved greatness-Nick- Has achieved greatness-Nick- Has achieved greatness-Nick- Has achieved greatness-Nick- Has achieved greatness-Nick- Has achieved greatness-Nick- Has achieved greatness-Nick- Has achieved greatness-Nick- Has achieved greatness-Nick- Has achieved greatness-Nick- Has achieved greatness
 


Member of the Month
September 2007
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, 08:25 AM   #8 (permalink)
Senior Member
 
Scott's Avatar
Join Date: Jun 2003
Location: UK
Posts: 3,547
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
 

Member of the Month
February 2005

PHP Code:
if(in_array($var, array("this""that""something else"))) { 
Scott is offline  
Old 08-27-2006, 08:25 AM THREAD STARTER               #9 (permalink)
Soon to be RICHdoggie!
 
PoorDoggie's Avatar
Join Date: Jan 2005
Location: UK
Posts: 2,408
PoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nice
 



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
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, 08:28 AM   #10 (permalink)
NamePros Regular
 
bliss's Avatar
Join Date: Dec 2005
Posts: 951
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 allbliss is a name known to allbliss is a name known to all
 




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.
????: NamePros.com http://www.namepros.com/showthread.php?t=231959

-
Last edited by bliss; 08-27-2006 at 08:36 AM.
bliss is offline  
Old 08-27-2006, 09:29 AM   #11 (permalink)
I'll do it
 
-Nick-'s Avatar
Join Date: Dec 2005
Location: India
Posts: 6,927
-Nick- Has achieved greatness-Nick- Has achieved greatness-Nick- Has achieved greatness-Nick- Has achieved greatness-Nick- Has achieved greatness-Nick- Has achieved greatness-Nick- Has achieved greatness-Nick- Has achieved greatness-Nick- Has achieved greatness-Nick- Has achieved greatness-Nick- Has achieved greatness
 


Member of the Month
September 2007
Adoption
PHP Code:
if($var == "this")
{
if(
$var == "that")
{
if(
$var == "something else")
????: NamePros.com http://www.namepros.com/showthread.php?t=231959
{
  
Statements here.
}
}

^^^ Nested If's
-Nick- is offline  
Old 08-27-2006, 05:45 PM   #12 (permalink)
NamePros Regular
 
Tree's Avatar
Join Date: Feb 2006
Location: Atlanta, GA, USA
Posts: 335
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, 02:25 AM THREAD STARTER               #13 (permalink)
Soon to be RICHdoggie!
 
PoorDoggie's Avatar
Join Date: Jan 2005
Location: UK
Posts: 2,408
PoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie 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, 03:29 AM   #14 (permalink)
Senior Member
Join Date: May 2003
Posts: 2,187
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, 03:47 AM   #15 (permalink)
NamePros Regular
 
Tree's Avatar
Join Date: Feb 2006
Location: Atlanta, GA, USA
Posts: 335
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, 07:36 AM THREAD STARTER               #16 (permalink)
Soon to be RICHdoggie!
 
PoorDoggie's Avatar
Join Date: Jan 2005
Location: UK
Posts: 2,408
PoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie 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, 08:26 AM   #17 (permalink)
I'll do it
 
-Nick-'s Avatar
Join Date: Dec 2005
Location: India
Posts: 6,927
-Nick- Has achieved greatness-Nick- Has achieved greatness-Nick- Has achieved greatness-Nick- Has achieved greatness-Nick- Has achieved greatness-Nick- Has achieved greatness-Nick- Has achieved greatness-Nick- Has achieved greatness-Nick- Has achieved greatness-Nick- Has achieved greatness-Nick- Has achieved greatness
 


Member of the Month
September 2007
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, 08:33 AM   #18 (permalink)
NamePros Regular
 
Noobie's Avatar
Join Date: Feb 2006
Location: Montreal, Quebec, Canada
Posts: 324
Noobie is on a distinguished road
 



I misread the original question
Originally Posted by Scott
PHP Code:
if(in_array($var, array("this""that""something else"))) { 
????: NamePros.com http://www.namepros.com/showthread.php?t=231959
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, 05:29 AM THREAD STARTER               #19 (permalink)
Soon to be RICHdoggie!
 
PoorDoggie's Avatar
Join Date: Jan 2005
Location: UK
Posts: 2,408
PoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nice
 



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'] == || $_GET['month'] == || $_GET['month'] == || $_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


Liquid Web Smart Servers  
All times are GMT -7. The time now is 10:07 PM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger