By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,647 Members | 1,178 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,647 IT Pros & Developers. It's quick & easy.

I think this is a simple question...

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.