Ferenc Kovacs wrote (on 13/08/2014):
not sure what do you mean here, multiple default cases can be reached the
same way as any other duplicated case branch(as shown in my snippet in the
mail you replied to).

If you're talking about http://3v4l.org/eZdPU then those duplicates are definitely being silently ignored. Remember that the switch statement is just a glorified goto, so execution starts at the first matching case and then carries on until you hit a "break" or the end of the switch block.

In that example, it finds the first "case 'foo':", does a goto, and at that point all the other labels are completely irrelevant; the 4 echo statements are executed in order as though the other labels didn't exist.

If you feed it something other than 'foo', then the behaviour being discussed kicks in: for whatever reason, the *last* default label is selected as the target of the goto, so only the last echo is executed: http://3v4l.org/TfYiQ

Interestingly, HHVM apparently behaves the same on that example, although others mentioned it selecting the first rather than last default label.

not sure about the multiple defaults, but I'm fairly sure that there are
code out there which uses switch blocks as state-machines, and changes the
switch variable in the case blocks, and have duplicated case blocks for
executing code after the switch variable reached it's final form.

Multiple cases do not make any difference here; the label to jump to is selected only once, so if you want to change the state and jump to a different label, you have to wrap the switch in a loop, and evaluate it again. If you specify multiple labels matching the same state, the second one can never be reached. http://3v4l.org/i3746

--
Rowan Collins
[IMSoP]

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to