Solved

switch statements???

Posted on 2002-07-08
5
295 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.

726 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