Structure Of A Switch

free web hosting
Free Web Hosting > Computers & Tech > Programming > Scripting > PHP

Structure Of A Switch

Habble
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:

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


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


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


CODE
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.

 

 

 


Reply

vizskywalker
It's been a while since I've used switch in PHP, but I imagine it uses standard C notation, which goes as follows:
CODE
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

 

 

 


Reply

Jimmy89
According to www.php.net the switch structure is

CODE
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 www.php.net/switch to read more about it!
-jimmy

Reply

pyost
The switch structure is almost exactly how vizskywalker described it.

CODE
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.

CODE
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, www.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 smile.gif

Reply

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

CODE
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
CODE
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

Reply

pyost
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 smile.gif

Reply


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

Similar Topics

Keywords : Structure Switch

  1. Automated File Structure Creation Script - As Requested By Mark420 (3)



Looking for structure, switch






*SIMILAR VIDEOS*
Searching Video's for structure, switch
advertisement




Structure Of A Switch