Solved

switch statements???

Posted on 2002-07-08
5
292 Views
Last Modified: 2008-02-07
hai all,

I wanted to use or operator like below in switch statement. Is it possible or is there any other way of doing the same.

switch($gselect){

 case 'bimc' || 'buic':{
 //some scripts
 }
// other switch case statemnts.
 
I don't prefer to split in to two case statements.

NOTE: but using this way Iam not able to execute statemnts within that case.

Any body can give feedback !!!!!!!!!

manju



0
Comment
Question by:manju70
  • 3
5 Comments
 
LVL 7

Expert Comment

by:axis_img
ID: 7139521
It is really not good programming practice to use this method. This should probably use a normal if() statement, as it is meant for controlling such expressions. With that said, give this a try.

<?
$gselect = "buic";

switch($gselect) {
        case ( ($gselect == "bimc" || $gselect == "buic") ? $gselect:1): print "$gselect matched '$
        case ( ($gselect == "boot" || $gselect == "boat") ? $gselect:1): print "$gselect matched '$
        default: print "$gselect did not match any of the above.";
}
?>


Try that script out and let me know how it works for you. Change the $gselect value to some of the other values, such as "bimc", "boot", etc.. And also try changing it to a value that is _NOT_ in one of the case statements.

Regards,
Barry
0
 
LVL 7

Accepted Solution

by:
axis_img earned 35 total points
ID: 7139523
Sorry... Here is the "uncut" version of the script above. My pico editor was too small, so the end of the lines got cut off.

<?
$gselect = "boat";

switch($gselect) {
        case ( ($gselect == "bimc" || $gselect == "buic") ? $gselect:1): print "$gselect matched 'bimc' or 'buic'<br>"; break;
        case ( ($gselect == "boot" || $gselect == "boat") ? $gselect:1): print "$gselect matched 'boot' or 'boat'<br>"; break;
        default: print "$gselect did not match any of the above.";
}
?>
0
 
LVL 5

Expert Comment

by:Hamlet081299
ID: 7139659
switch continues through until it finds a break, so you simply list the cases one after another as in the following example...

switch($gselect){

case 'bimc':
case 'buic':
  //some commands
  ...
  break;

// other switch case statements.

}
0
 
LVL 7

Expert Comment

by:axis_img
ID: 7139697
Argh! I specifically tested that first before posting the cryptic hack above, but I was getting errant results. It was not matching the first case, but it was due to an error in my code that I just found. I could have sworn that I used that method before too, so I was surprised that it was not working. My bad. :)

Anyway... the example posted by Hamlet is much better suited for your needs. It's much cleaner and the common way of doing this sort of thing (other than just using an if() branch).

Regards,
Barry
0
 

Author Comment

by:manju70
ID: 7145280
as per axis_img comment, I was able to get my requirement
by using below code:

case ( ($gselect == "bimc" || $gselect == "buic")) :

thanks for all of your efforts.
manju
0

Featured Post

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question