473,394 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Switch question

Jon
In some languages, you can do the following in a Switch statement...is there
a way to do this in PHP? Thanks in advance

switch($voteraction){
case '1','2','3','4','5','6','7','8','9','10':
$db_set .= "Rating = $voteraction, ";
break;
}

or do I have to the following:
switch($voteraction){
case '1':
$db_set .= "Rating = $voteraction, ";
break;
case '2':
$db_set .= "Rating = $voteraction, ";
break;
case '3':
$db_set .= "Rating = $voteraction, ";
break;
etc....
}

--

*********************
Jon Rosenberg
www.DeanForAmerica.com
www.OhioForDean.org
Jul 17 '05 #1
4 1786
On Sat, 15 Nov 2003 19:16:05 GMT, "Jon" <ru******@msn.com> wrote:
In some languages, you can do the following in a Switch statement...is there
a way to do this in PHP? Thanks in advance

switch($voteraction){
case '1','2','3','4','5','6','7','8','9','10':
$db_set .= "Rating = $voteraction, ";
break;
}

or do I have to the following:
switch($voteraction){
case '1':
$db_set .= "Rating = $voteraction, ";
break;
case '2':
$db_set .= "Rating = $voteraction, ";
break;
case '3':
$db_set .= "Rating = $voteraction, ";
break;
etc....
}


switch($voteraction){
case '1':
case '2':
case '3':
$db_set .= "Rating = $voteraction, ";
break;
}

http://uk2.php.net/switch

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 17 '05 #2
Jon wrote:
In some languages, you can do the following in a Switch statement...is there
a way to do this in PHP? Thanks in advance

switch($voteraction){
case '1','2','3','4','5','6','7','8','9','10':
$db_set .= "Rating = $voteraction, ";
break;
}


an alternative could be

if(in_array($voteraction,array('1','2','3','4','5' ,'6','7','8','9','10'))) {
// whatever
}
elseif(...)

another alternative could be a regular expression match but my knowledge
is very shakey in that area. Perhaps something like the following
(someone with regex knowledge would have to confirm this).

if (preg_match("/^\d.$/", $voteraction)) {
// whatever
}

Jul 17 '05 #3
Andy Hassall (73.075% quality rating):

switch($voteraction){
case '1':
case '2':
case '3':
$db_set .= "Rating = $voteraction, ";
break;
}


If you have to do a huge number of cases, a somewhat less classy-looking
way would be (assuming voteraction is an int):

switch(true) {
case (1<=$voteraction && $voteraction<=10):
$db_set .= "Rating = $voteraction, ";
break;
...

If it's not an int, you could cast it to one first, use floor(), or whatever.

Of course, if you're doing the above, you're probably better off with
if-else blocks.

/joe
--
Andy James practically disrespects the phatcave. In Krispy Kreme, the
triple-poorly-executed emo kid is uberordinary.
Jul 17 '05 #4
"Jon" <ru******@msn.com> wrote in message news:<VX*******************@news2.news.adelphia.ne t>...
In some languages, you can do the following in a Switch statement...is there
a way to do this in PHP? Thanks in advance

switch($voteraction){
case '1','2','3','4','5','6','7','8','9','10':
$db_set .= "Rating = $voteraction, ";
break;
}


I'd use:

switch($voteraction){
case 1: case 2: case 3: case...:
$db_set .= "Rating = $voteraction, ";
break;
}

Cheers,

Chris.
Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Myster Ious | last post by:
Polymorphism replaces switch statements, making the code more compact/readable/maintainable/OO whatever, fine! What I understand, that needs to be done at the programming level, is this: a...
6
by: Aristotelis E. Charalampakis | last post by:
Hi all, this is a newbie question :-) I was wondering if there was a way to use the switch statement in a manner that each case statement includes more that a simple value. i.e.: switch (...
35
by: Thomas Matthews | last post by:
Hi, My son is writing a program to move a character. He is using the numbers on the keypad to indicate the direction of movement: 7 8 9 4 5 6 1 2 3 Each number has a direction except...
2
by: jr | last post by:
I have a niggle with the Switch function I have a querey which has a column with 3 digit values of which there are about 20 which are unique. These are meaningless to the user and so using...
18
by: Minti | last post by:
I was reading some text and I came across the following snippet switch('5') { int x = 123; case '5': printf("The value of x %d\n", x); break; }
13
by: William Stacey | last post by:
Using the following code sample: public byte Get() { // <= Possible to switch Here?? lock(syncLock) { //Do something in Get(). } }
3
by: pgraeve | last post by:
I am a convert from VB to C# so bear with me on this "conversion" question C# switch statement seems to be the closest relative to VB's Select Case. I used VB's Select Case statement liberally. ...
13
by: Michael Griebe | last post by:
Simple question. I am optimizing some C++ code and I'd like to know which is faster (or if there is any difference at all) between using a switch statement or nested else-ifs. I'm partial to...
11
by: ME | last post by:
In C# the following code generates a compiler error ("A constant value is expected"): public void Test(string value) { switch (value) { case SimpleEnum.One.ToString(): MessageBox.Show("Test...
12
by: | last post by:
Is it fine to call another method from Switch? Eg. Switch (stringVar) { case ("a"): somVar = "whatever"; Another_Method(); //call another method return;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.