Re: [RFC] Disallow multiple default blocks in a single switch statement
| From: | Rowan Collins | Date: | Wed, 13 Aug 2014 13:55:53 +0000 |
| Subject: | Re: [RFC] Disallow multiple default blocks in a single switch statement | ||
| References: | 1 2 3 4 5 6 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-76486@lists.php.net to get a copy of this message | ||
Ferenc Kovacs wrote (on 13/08/2014):
I think you misunderstood that part, I was thinking about a state machine like this: 1. function doStuff(){ 2. switch($state){ 3. case OPENDOOR: 4. if(!opendoor()){ 5. break;The assignments to $state in that code do not make any difference to execution, control simply flows forwards whenever there is no "break". $state is only tested once, to select the initial label to jump to, so no amount of reassigning it, or adding duplicate labels, can ever cause a second jump. I'm tempted to write a PHP script that emulates the switch execution by building a set of if and goto statements, which might make some of this behaviour clearer (not just for this discussion, but for other confusion I've seen elsewhere), but I don't have time right now.6. } 7. $state = SITDOWN;8. case SITDOWN: 9. if(!sitdown()){ 10. break;11. } 12. $state = SIPWHISKEY;13. case SIPWHISKEY:14. sipwhiskey();15. } 16. } where you modify the switch variable in one case and fall through into another. as I mentioned I don't have a reasonable use-case for multiple defaults, but I can see some for multiple case labels in general, and I don't think that it is better to have different behavior for case and default in this regard.