Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:75985 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 18797 invoked from network); 23 Jul 2014 23:04:07 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 23 Jul 2014 23:04:07 -0000 Authentication-Results: pb1.pair.com header.from=rowan.collins@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=rowan.collins@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.212.174 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 209.85.212.174 mail-wi0-f174.google.com Received: from [209.85.212.174] ([209.85.212.174:55324] helo=mail-wi0-f174.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 7D/00-17629-46F30D35 for ; Wed, 23 Jul 2014 19:04:05 -0400 Received: by mail-wi0-f174.google.com with SMTP id d1so8707396wiv.7 for ; Wed, 23 Jul 2014 16:04:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=yMW0eLeE1X7ZKAb+QIQkoCqv04LR1omYxh2xTn9ck3Q=; b=Q40vXkYXgSvsvUm1so2pDJxpOfEomhrUzZEU1bbuD7wXm2MgUVs7Avz/5Oe3IW7rOO 7TRrI3kQ6ckpC6i5rGWBEsaTnLbNOwkMsjwU7jqU2se8GdQnjPubIW6maFCcVFPVfuo+ H7CJAoKybONQCjUCd8oFGxygnLXCQNQiwY2WtLA9Rkjw0DIVZNFrWvdDl67q5rNUXRNo NmMSx7/xmOnPwRgt4KBT6APctVwffr03Cs9xBCsLOgj1/cjTkNMZ2h29t08ig034nLmT zLUlT1ebtPjj6xRuIpRc4yK3CYIZ4x14fwCqui/uXAI3pIfbcEhuS9KfEqFaJp2zq5A6 kF2Q== X-Received: by 10.194.8.35 with SMTP id o3mr6216599wja.3.1406156644190; Wed, 23 Jul 2014 16:04:04 -0700 (PDT) Received: from [192.168.0.2] (cpc19-brig17-2-0-cust25.3-3.cable.virginm.net. [81.101.201.26]) by mx.google.com with ESMTPSA id d4sm14697010wiy.13.2014.07.23.16.04.03 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 23 Jul 2014 16:04:03 -0700 (PDT) Message-ID: <53D03F5E.6040104@gmail.com> Date: Thu, 24 Jul 2014 00:03:58 +0100 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 MIME-Version: 1.0 To: internals@lists.php.net References: <53CFA55D.2030105@lsces.co.uk> In-Reply-To: <53CFA55D.2030105@lsces.co.uk> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] PHP Language Specification From: rowan.collins@gmail.com (Rowan Collins) 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]