|
|
|
|
![]() ![]() |
Aug 2 2007, 06:37 AM
Post
#1
|
|
|
Premium Member 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. |
|
|
|
Aug 2 2007, 07:49 AM
Post
#2
|
|
|
Techno-Necromancer 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{ 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.case: Whatever; break; case: Whatever; break; default: Whatever; } ~Viz |
|
|
|
Aug 2 2007, 12:31 PM
Post
#3
|
|
|
Living at the Datacenter 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 |
|
|
|
Aug 2 2007, 12:35 PM
Post
#4
|
|
|
Nenad Bozidarevic 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 |
|
|
|
Aug 2 2007, 04:51 PM
Post
#5
|
|
|
Techno-Necromancer 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. 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 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. CODE switch (expression) { 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.case A: case B: stuff break; case C: stuff break; default: } ~Viz |
|
|
|
Aug 2 2007, 05:53 PM
Post
#6
|
|
|
Nenad Bozidarevic 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
|
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 2nd December 2008 - 12:50 PM |