472,980 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,980 software developers and data experts.

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 2490
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: aredo3604gif | last post by:
On Sun, 10 Apr 2005 19:46:32 GMT, aredo3604gif@yahoo.com wrote: >The user can dynamically enter and change the rule connection between >objects. The rule is a "<" and so given two objects: >a <...
7
by: abcd | last post by:
I am trying to set up client machine and investigatging which .net components are missing to run aspx page. I have a simple aspx page which just has "hello world" printed.... When I request...
6
by: Brian | last post by:
Hello, I was having a problem saveing images that I edited with a vb.net program. Every time I would save the image I would receive a GDI+ error. I tried everything that I could think of...
4
by: dba_222 | last post by:
Dear Experts, Ok, I hate to ask such a seemingly dumb question, but I've already spent far too much time on this. More that I would care to admit. In Sql server, how do I simply change a...
29
by: GhostInAK | last post by:
I'm seeing a terribly distubing number of questions that have no purpose in existing. As an example: How do I change the position of a stream? Hmm.. Could it be some method on the stream...
1
by: Ronald S. Cook | last post by:
I have an ASPX page wherein I receive an ID of a file to play. http://localhost/fwi/mediaplayer.aspx?id=3 When I go to a browser and type in the above, it works fine. But when I change the 3...
5
by: eric dexter | last post by:
I am just trying to acess a function in wordgrid (savefile) to a button that is defined in TestFrame. I can't seem to make it work I either get an error that my variable isn't global or it makes...
0
by: DanWeaver | last post by:
cant find answer to this and spent 2 hours looking including in this newsgroup! I have a web user control in my project (.ascx) the associated (vb) code (.ascx.vb) doesnt provide intellisense...
10
by: Phillip Taylor | last post by:
Hi guys, I'm looking to develop a simple web service in VB.NET but I'm having some trivial issues. In Visual Studio I create a web services project and change the asmx.vb file to this: Imports...
6
by: Lasse Reichstein Nielsen | last post by:
Max <adsl@tiscali.itwrites: Not really. It shows that a particularly nave implementation of a conversion from XML to JSON doesn't work well. What if the conversion of <e> some
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.