Forum Moderators: coopster

Message Too Old, No Replies

If variables equals this, this, this or this

Is there a better way of writing this?

         

Jeremy_H

2:18 am on May 12, 2006 (gmt 0)

10+ Year Member



Is there a more elegant way of stating this:

if(($c="sg")¦¦($c="tw")¦¦($c="vi")¦¦($c="ye")¦¦($c="zw")){...}

Thanks

eelixduppy

2:24 am on May 12, 2006 (gmt 0)



you could try this:

$array = array("sg","tw","vi","ye","zw");
if(array_search($c, $array))
{ //do something }

eelix

Jeremy_H

3:10 am on May 12, 2006 (gmt 0)

10+ Year Member



Thanks eelix for the reply.

That would work, but it seems somewhat code/processor heavy.

Is there not something like:

if($c=="sg"¦¦"tw"¦¦"vi"¦¦"ye"¦¦"zw"){...}

eelixduppy

3:26 am on May 12, 2006 (gmt 0)



How about switch statements?

switch($c) {

case "sg":
case "tw":
case "vi":
case "ye":
case "zw":

//do something
break;
}

Still too wordy for you? I don't think there is a faster way using logical operators [us3.php.net]

eelix

coopster

11:55 am on May 12, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Personally, I would go the array route and use either array_search [php.net] or in_array [php.net].

Jeremy_H

4:23 pm on May 12, 2006 (gmt 0)

10+ Year Member



Thank you both for your help. Looks like in_array is the way to go.

Thanks.