Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> Structure Of A Switch
Habble
post Aug 2 2007, 06:37 AM
Post #1


Premium Member
Group Icon

Group: [HOSTED]
Posts: 286
Joined: 17-June 07
From: Tasmania
Member No.: 22,699



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.
Go to the top of the page
 
+Quote Post
vizskywalker
post Aug 2 2007, 07:49 AM
Post #2


Techno-Necromancer
Group Icon

Group: Members
Posts: 1,018
Joined: 13-January 05
From: The Net
Member No.: 2,127



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
Go to the top of the page
 
+Quote Post
Jimmy89
post Aug 2 2007, 12:31 PM
Post #3


Living at the Datacenter
Group Icon

Group: [HOSTED]
Posts: 708
Joined: 30-June 06
From: Australia
Member No.: 14,219
myCENTs:76.93



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

This post has been edited by Jimmy89: Aug 2 2007, 12:32 PM
Go to the top of the page
 
+Quote Post
pyost
post Aug 2 2007, 12:35 PM
Post #4


Nenad Bozidarevic
Group Icon

Group: [MODERATOR]
Posts: 1,048
Joined: 7-November 05
From: Belgrade, Serbia
Member No.: 9,500
myCENTs:89.26



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
Go to the top of the page
 
+Quote Post
vizskywalker
post Aug 2 2007, 04:51 PM
Post #5


Techno-Necromancer
Group Icon

Group: Members
Posts: 1,018
Joined: 13-January 05
From: The Net
Member No.: 2,127



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
Go to the top of the page
 
+Quote Post
pyost
post Aug 2 2007, 05:53 PM
Post #6


Nenad Bozidarevic
Group Icon

Group: [MODERATOR]
Posts: 1,048
Joined: 7-November 05
From: Belgrade, Serbia
Member No.: 9,500
myCENTs:89.26



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
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Wireless Networking And Switch(7)
  2. The Big Guide To Web Design Part 1 Of 4(7)
  3. Apple Switch To Intel From Ibm?(10)
  4. Switching To Linux(8)
  5. VB.NET: Switch Regional Language Automatically(1)
  6. Switching From Fp To Dreamweaver(6)
  7. Switch To Firefox, Today !(24)
  8. How To Share Inernet Connection Over Switch(2)
  9. How Do I Switch To Temporary Paid Hosting ?(3)
  10. Switch Network Settings With Batch Files(18)
  11. Want To Switch, But What The Heck Is "Alpha", "PPC" Etc?(8)
  12. PC Takes Ages To Boot(8)
  13. Switch To Trap17(4)
  14. IBM Offering $20,000 For Exchange Users To Switch(2)
  15. Lesson #2 - Switch Statement In C#(1)
  1. What Made You Switch To Linux?(61)
  2. Structure Of Webpage(6)
  3. Lock Down Switch Port To Increase Network Security(0)
  4. Baylan Network Switch Problem(1)
  5. How Do I Use Linux To Create A Switch From Multiple Ethernet Ports(16)
  6. Automated File Structure Creation Script(3)
  7. How To Keep The Internet Out From A Network ?(9)
  8. Task Switch Powertoy, Enhance Your Alt+tab Key Combination!(16)
  9. Network Outage. Distribution Switch Ddosed.(5)
  10. Sandisk Memory Card Write Protection(13)
  11. Tracing Broadcast Storms(1)
  12. A Light Switch For The Brain(2)
  13. Keyboard Video Mouse Switch(2)


 



- Lo-Fi Version Time is now: 2nd December 2008 - 12:50 PM