If statement with multiple values

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

If statement with multiple values

Post by mhouldridge »

Hi,

I have the following if statement which checks on other values from a list box;

Code: Select all

if ($list == "asset", "customer", "title", "oslicense") {	
	$result = mysql_query("SELECT * FROM dedicated WHERE $list LIKE '%$search%' ORDER BY asset");
	}
I get unexpected"," error.... any ideas?
User avatar
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

Post by mhouldridge »

do I need a switch statement?
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

you might want to have a look at
http://php.net/in-array
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Re: If statement with multiple values

Post by BDKR »

mhouldridge wrote:Hi,

I have the following if statement which checks on other values from a list box;

Code: Select all

if ($list == "asset", "customer", "title", "oslicense") {	
	$result = mysql_query("SELECT * FROM dedicated WHERE $list LIKE '%$search%' ORDER BY asset");
	}
I get unexpected"," error.... any ideas?
You can't compare multiple values at one shot like that. If you want to do it with one if statement, then it's better to do ...

Code: Select all

if ($list == "asset" || $list == "customer" || $list == "title" ||  $list == "oslicense") 
    {	/* do stuff  */  }
A switch would work here too, but the use of one or the other depends on a lot of other things I won't get into
here. Especially considering some of it may be a matter of opinion.

Now if what you really want is to check each of those items in the list (asset, customer, title, osilcense), then just put them in an
array and iterate.

Code: Select all

$list=array('asset', 'customer', 'title', 'osilcense')
foreach($list as $item)
    { /* do stuff with $item */ }
Cheers
Post Reply