On 23/07/2014 13:06, Lester Caine wrote:
I have always put the 'default:' as the last item in the list since it's
what is left after processing all the other options and one may or may
not have fallen out.
This is intuitive, and arguably good coding style, but not necessary as
switch statements are implemented in PHP.
Anyway, having then found the right page, some of the subtleties
documented in the spec are not covered in the manual. In fact I don't
think that what is described in the spec is actually what happens in
code? This is perhaps the reason that a formal spec is long overdue. The
manual says the statements are actioned in order, and certainly that is
what I expect so I can add bits in the flow through, but does the switch
statement know that a value is not listed as a case prior to scanning
the code?
I think the specification does quite a good job of describing the
abstract behaviour. Whether this is literally how it is implemented in
the engine doesn't actually matter, as long as the result is *as though*
it is the case.
These seem to be the key points:
- "If a switch contains more than one case label whose values compare
equal to the controlling expression, the first in lexical order is
[considered] the match." (this is the only way in which the ordering of
labels makes any difference; a compiled form might involve some form of
lookup table, but it must follow this rule in the case of duplicate "keys")
- "If there is no match, then if there is a default label, control
transfers to the statement following that" (i.e. the selection of a
default label happens after all other options have been exhausted, not
when the default label is encountered)
The fact that code falls through from one case to another is separate
from the selection of the label to *start* execution at. It is
equivalent to think of all case labels being removed from the code as
soon as one is selected: control continues until it reaches a "break"
statement or the end of the switch.
Here's an example to play around with: http://3v4l.org/rm72I
P.S. Whatever form this spec is eventually distributed in, I hope it
doesn't contain whatever markup/misfeature is causing Adobe Reader to
insert quote"marks"between"every"word when I copy and paste from it!
--
Rowan Collins
[IMSoP]
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php