Solved

switch statements???

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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 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 …

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now