Solved

switch statements???

Posted on 2002-07-08
5
294 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

749 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