Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-28-2007, 10:28 PM   PM User | #1
ptmuldoon
Regular Coder

 
Join Date: Feb 2005
Posts: 659
Thanks: 5
Thanked 14 Times in 14 Posts
ptmuldoon is on a distinguished road
Using if statement with switch/case

Is it possible to add an if statement inside of switch case? Below is a small piece of a pretty long switch statement that has about 12 different cases. But I only want it to change/switch cases if certain conditions are met for each particular case.

Can I just add an if statement within the case?

PHP Code:
switch($current_state){
    case 
'forcetrade'$next_state 'forceplace';
                        break;
    case 
'forceplace'$next_state 'attacking';
                        break;

ptmuldoon is offline   Reply With Quote
Old 02-28-2007, 10:38 PM   PM User | #2
bcarl314
Mega-ultimate member


 
Join Date: Jun 2002
Location: Winona, MN - The land of 10,000 lakes
Posts: 1,855
Thanks: 1
Thanked 45 Times in 42 Posts
bcarl314 will become famous soon enough
Yep, you can do all sorts of fun stuff...

Code:
switch($somevar) {
	case "option1":
	case "option2":
		//this will run for options 1 and 2
		if($something == 2) {
			//do for 2
		}
		else {
			//do for not 2
		}
		break;
	case "option3":
		//option 3
		break;
	default:
		//everything else, this is a catch all
}
I think you can even do things like <4 for your cases as well, but I've never tried it.
bcarl314 is offline   Reply With Quote
Old 02-28-2007, 11:34 PM   PM User | #3
ptmuldoon
Regular Coder

 
Join Date: Feb 2005
Posts: 659
Thanks: 5
Thanked 14 Times in 14 Posts
ptmuldoon is on a distinguished road
Thanks. Then if I understand correctly, I think the below code is correct?
PHP Code:
case 'initial'
            if (
$game_capital == Yes){
                
$next_state 'capital';
            } else {
                
$next_state 'inactive';
            }
                break;
case 
'capital'$next_state 'inactive'
I ask because I haven't gotten my $next_state variable to work correctly. If the above is correct, than I likely need to amend the code somewhere else. Sort of a trial and error on my part to implement some new stuff.
ptmuldoon is offline   Reply With Quote
Old 02-28-2007, 11:49 PM   PM User | #4
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
Strings (such as your string 'Yes') must be enclosed in quotes. If you are trying to set a boolean value, use TRUE or 1.
__________________
Fumigator is offline   Reply With Quote
Old 03-01-2007, 02:21 AM   PM User | #5
neillglobal
New to the CF scene

 
Join Date: Feb 2007
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
neillglobal is an unknown quantity at this point
This is a question I'd like to know the answer to aswell. I understand what you're trying to do.

If your $xxxstatexxx changes are sequential then you can just run the switch without breaks, and so the switch begins to run the code at certain points depending on the condition. I get that... no problem.... BUT what if you want to skip the next case go to another one x cases down the line..... That's what I want to know. That would be useful.
neillglobal is offline   Reply With Quote
Old 03-01-2007, 03:30 PM   PM User | #6
aedrin
Senior Coder

 
Join Date: Jan 2007
Posts: 1,648
Thanks: 1
Thanked 58 Times in 54 Posts
aedrin will become famous soon enough
If you forget a break (on purpose or not), then the next case statements will not be evaluated. It will automatically fall through and execute the code. Otherwise the break statement wouldn't be necessary.

Quote:
BUT what if you want to skip the next case go to another one x cases down the line..... That's what I want to know. That would be useful.
You can't skip X cases from within a case.

It sounds to me like you need a different setup. Consider using a for/while statement with a switch to achieve what you need. It depends on your situation though.
aedrin is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:43 PM.


Advertisement
Log in to turn off these ads.