Jump to content
xisto Community
iGuest

Structure Of A Switch

Recommended Posts

Hi, I've been having trouble with switches. I've never used them in PHP before, and I can't quite get the structure right. This is what i've tried:

switch ($variable){  case (whatever)  {	//Whatever  }  case (whatever)  {	//Whatever  }}

switch ($variable){  case (whatever):  {	//Whatever  }  case (whatever):  {	//Whatever  }}

switch ($variable){  case (whatever) : Whatever  case (whatever) : Whatever}

switch ($variable){  case (whatever) : { Whatever }  case (whatever) : { Whatever }}

Can someone please tell me the proper way to structure a switch? Because I've ended up using if statements instead, which take a lot longer.

Share this post


Link to post
Share on other sites

It's been a while since I've used switch in PHP, but I imagine it uses standard C notation, which goes as follows:

switch{	case:		Whatever;		break;	case:		Whatever;		break;	default:		Whatever;}
In some languages, the default is optional, but in others it is not. The break statements are required to keep one case from flowing into the next. Hope that helps. If it doesn't, perhaps you could be more specific, by telling us how it is not working (error message? wrong answer? etc.) which is general good practice anyway. Don't make us guess. Also, try doing a web search, many simple tutorials on the web will show you how to do this. Try to keep this forum for questions that are not as easy to find answers to.

~Viz

Share this post


Link to post
Share on other sites

According to http://php.net/ the switch structure is

switch ($i) {case 0:	echo "i equals 0";	break;case 1:	echo "i equals 1";	break;case 2:	echo "i equals 2";	break;}?>
Which is pretty much what vizskywalker said, but goto http://php.net/manual/en/control-structures.switch.php to read more about it!
-jimmy
Edited by Jimmy89 (see edit history)

Share this post


Link to post
Share on other sites

The switch structure is almost exactly how vizskywalker described it.

 

switch (expression) {   case value1:		 something;		 break;   case value2:		 something;		 break;   case value3:		 something;		 break;   default:		 something;}
When the switch construction is entered, PHP checks whether one of the cases matches the expression value. If it does, it executes everything from that case to a break, or the end of the switch. Therefore, if there are no break, and expression = value1, all cases will be executed! That is why breaks are so important. Obviously, you don't need a break for the default case (which is executed when none of the other values match the expression, and it is optional), as it is followed by the switch end.

 

Furthermore, while I am not completely sure about this, I believe that you can use multiple values for a single case, separated by commas.

 

switch (expression) {   case value1, value2:		 something;		 break;   case value3:		 something;		 break;   default:		 something;}
This is usually the case with all the programming languages, but there doesn't seem to be such an example on php.net.

 

Also, if you have any problems with PHP, http://php.net/ is a great site that will solve 90 per cent of all your problems. I will even say that it is the best official resource when compared to other languages or applications.

 

~edit~

 

Aah, Jimmy beat me to it :P

Share this post


Link to post
Share on other sites

Furthermore, while I am not completely sure about this, I believe that you can use multiple values for a single case, separated by commas.

switch (expression) {   case value1, value2:		 something;		 break;   case value3:		 something;		 break;   default:		 something;}


This is usually the case with all the programming languages, but there doesn't seem to be such an example on php.net.
I don't know if PHP supports that, but in many languages, including PHP, the seemingly preferred way to have multiple cases do the same thing is
switch (expression) {	case A:	case B:		stuff		break;	case C:		stuff		break;	default:}
Instead of saying "case A, B:" you simply layer the cases. This is why cases run line by line once the appropriate case is encountered. Otherwise, requiring the breaks really is a silly concept. Also, apparently in PHP, the break statements can be replaced with "continue" statements, but I would advise against it as it can lead to confusion.

~Viz

Share this post


Link to post
Share on other sites

As I said, I am not absolutely positive whether you can do so with PHP or not, but to me it seems more logical than the option you mentioned. It might just be a Pascal habit, though :P

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.