?
Solved

switch statements???

Posted on 2002-07-08
5
Medium Priority
?
299 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 105 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

650 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