468,104 Members | 1,320 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,104 developers. It's quick & easy.

I think this is a simple question...

I have a variable ($x) that can have 50 different (string) values. I want
to check for 7 of those values and do something based on it ... as I see it
I have 2 options:

1) if (($x=="one") || ($x=="two") || ... || ($x=="seven")) ...

or

2) switch ($x){
case("one"):
case("two"):
...
}

It seems that there might be a more efficient way to do this?? Is there?

Alex
Jul 16 '05 #1
2 2236
Alexander Ross wrote:
I have a variable ($x) that can have 50 different (string) values. I want
to check for 7 of those values and do something based on it ... as I see
it I have 2 options:

1) if (($x=="one") || ($x=="two") || ... || ($x=="seven")) ...

or

2) switch ($x){
case("one"):
case("two"):
...
}

It seems that there might be a more efficient way to do this?? Is there?

Alex

You could do
if (pre_match('/^(one|two|three|four|five|six|seven)$/', $x))

but I think a lot of the time the switch method makes for more readable code.
It could be interesting to benchmark which is fastest, although I suspect there
isn't a lot of difference for most applications.

--
Matt Mitchell - AskMeNoQuestions
Dynamic Website Development and Marketing
Jul 16 '05 #2
Andy Hassall wrote:
On Mon, 04 Aug 2003 19:05:38 GMT, "Alexander Ross" <al******@bleen.net> wrote:

....
It seems that there might be a more efficient way to do this?? Is there?

Put possible values in an array, use array_search, for example.

....

array_search is for associated arrays, you should use

if (in_array($x, array('one','two',...)))
{

}

Regards,
Ondrej

Jul 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by abcd | last post: by
6 posts views Thread by Brian | last post: by
29 posts views Thread by GhostInAK | last post: by
1 post views Thread by Ronald S. Cook | last post: by
10 posts views Thread by Phillip Taylor | last post: by
6 posts views Thread by Lasse Reichstein Nielsen | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.