Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Checking if Variable ='s more than 1 thing
Old 01-04-2009, 07:31 AM Checking if Variable ='s more than 1 thing
lothop's Avatar
Ultra Talker

Posts: 303
Trades: 0
Hey all, I checked google but couldn't find what I was looking for.

I was wondering how to check a variable for 2 possibilities.
etc, if($variable = 'Bottom' or 'Top'){

Whats the right syntax use?
Thanks
__________________
Websites Created;
warscope.com
ratepayers.org.nz
lothop is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 01-04-2009, 07:36 AM Re: Checking if Variable ='s more than 1 thing
Insensus's Avatar
Ultra Talker

Posts: 487
Name: Mark Stegeman
Location: Netherlands, Europe
Trades: 0
if($variable == 'bottom' || $variable == 'top')
__________________
<?php ($helpfull>0)?$talkupation++ : '';?>
Insensus is offline
Reply With Quote
View Public Profile
 
Old 01-04-2009, 11:29 AM Re: Checking if Variable ='s more than 1 thing
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
or
PHP Code:
switch($variable){
  case 
'bottom':
    
//do something
    
break;
  case 
'top':
    
//do something else
    
break;
  case 
'center':
  case 
'middle':
    
//The 2 values goes for that part here
    
break;
  default:
    
//if nothing matches, the code in here goes
    
break;

I found a case statement to be more flexible than if/elseif/else in term of readability.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 01-04-2009, 07:53 PM Re: Checking if Variable ='s more than 1 thing
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
Quote:
Originally Posted by lothop View Post
Hey all, I checked google but couldn't find what I was looking for.

I was wondering how to check a variable for 2 possibilities.
etc, if($variable = 'Bottom' or 'Top'){

Whats the right syntax use?
Thanks
I always like to use the function in_array() for checking multiple values:

PHP Code:
if (in_array($needle, array('value1''value2''value3'))) . . . 
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 01-06-2009, 09:09 PM Re: Checking if Variable ='s more than 1 thing
lothop's Avatar
Ultra Talker

Posts: 303
Trades: 0
Is there and benefit of difference between the 2 ways?

I have found the most effective way
if (in_array($needle, array('value1', 'value2', 'value3')))

Thankyou for you input, just mgraphics way looks cleaner and simpler.

if($variable == 'bottom' || $variable == 'top')
looks basic because you are having to always put $variable in there again and again.
__________________
Websites Created;
warscope.com
ratepayers.org.nz

Last edited by lothop; 01-06-2009 at 09:18 PM..
lothop is offline
Reply With Quote
View Public Profile
 
Old 01-06-2009, 09:18 PM Re: Checking if Variable ='s more than 1 thing
Decaf's Avatar
Ultra Talker

Posts: 489
Name: Adam
Trades: 0
Personal Preference/OCD's
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Decaf is offline
Reply With Quote
View Public Profile Visit Decaf's homepage!
 
Old 01-10-2009, 09:38 PM Re: Checking if Variable ='s more than 1 thing
anderswc's Avatar
Super Talker

Posts: 132
Name: Will Anderson
Location: Terre Haute, IN
Trades: 0
in_array allows more optimization by the parser I'd think (in other words, it might be more efficient), but ultimately it comes down to personal preference as Decaf said. Also, it depends what you want to do. the using 'case' allows you to different things depending on the value of your variable. using in_array allows you to do the same thing as long as the value of your variable is any of the elements in the array.
__________________
Will Anderson

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
anderswc is offline
Reply With Quote
View Public Profile Visit anderswc's homepage!
 
Reply     « Reply to Checking if Variable ='s more than 1 thing
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.42259 seconds with 12 queries