semicolon terminator for switch cases

php.internals

Ilija Tovilo

6 years ago
Hi internals Looking through the language grammer I discovered that switch cases can also be terminated with a `;` instead of a `:`. ``` switch ($i) { case 1; return 1; default; return 2; } ``` https://3v4l.org/o7nD8 This is in fact documented: https://www.php.net/manual/en/control-structures.switch.php What's the reasoning behind this? I find it weird an inconsistent. Maybe something to deprecate in PHP 8.0. https://wiki.php.net/rfc/deprecations_php_8_0 Regards

Stas Malyshev

6 years ago
Hi!
> Maybe something to deprecate in PHP 8.0. > https://wiki.php.net/rfc/deprecations_php_8_0
Why? Whose life it'd make easier?
-- Stas Malyshev smalyshev@gmail.com

David Rodrigues

6 years ago
I agree with Tovilo. It is weird and confusing. Actually I never know that it was possible. And the ";" sounds like "it ends here", while ":" counds like "it does it ->". For me, "case 1;" will sounds like "skip case 1". Atenciosamente, David Rodrigues Em qui., 26 de mar. de 2020 às 17:14, Stanislav Malyshev < smalyshev@gmail.com> escreveu:

Stas Malyshev

6 years ago
Hi! On 3/26/20 1:26 PM, David Rodrigues wrote:
> I agree with Tovilo. It is weird and confusing. Actually I never know > that it was possible.
You seem to contradict yourself - if you never knew it was possible, how could you ever be confused by it?
-- Stas Malyshev smalyshev@gmail.com

Benoit SCHILDKNECHT

6 years ago
Le Thu, 26 Mar 2020 21:28:38 +0100, Stanislav Malyshev <smalyshev@gmail.com> a écrit:
> Hi! > > On 3/26/20 1:26 PM, David Rodrigues wrote: >> I agree with Tovilo. It is weird and confusing. Actually I never know >> that it was possible. > > You seem to contradict yourself - if you never knew it was possible, how > could you ever be confused by it? >
OK, I'm often lurking, but I have to react to this. I never knew it was possible too, but I am also confused by this. A semi-colon is terminal. It always is. "You see this set of instructions? Done.". But in this case, it is not. You see the problem?
-- L'absence de virus dans ce courrier électronique a été vérifiée par le logiciel antivirus Avast. https://www.avast.com/antivirus

Claude Pache

6 years ago
> Le 26 mars 2020 à 21:49, Benoit SCHILDKNECHT <bensor987@neuf.fr> a écrit : > > A semi-colon is terminal. Italways is. "You see this set of instructions? Done.". But in this case, itis not. You see the problem? >
No I don’t see the problem. Inside a switch construct, when you reach the end of an instruction (or of a set of instructions) that is not (resp. does not end with) a control-flow statement, you pass to the next one. I’m sure that everybody knows that you have to use an explicit control-flow statement like “break” or “return” if you want to be “done” before reaching the bottom of the switch construct. Or maybe people could misguess that “;” in that context is an odd way to mean “: break;”? —Claude

Claude Pache

6 years ago
> Le 26 mars 2020 à 19:37, Ilija Tovilo <ilija.tovilo@me.com> a écrit : > > What's the reasoning behind this? I find it weird an inconsistent. >
This is a manifestly a leftover of an old syntax. Take a look at the PHP/FI 2 manual: Language constructs: https://www.php.net/manual/phpfi2.php#lang <https://www.php.net/manual/phpfi2.php#lang> ``` <? if($a==5 && $b!=0 ); $c = 100 + $a / $b; endif;
>
``` and, in particular, switch construct: https://www.php.net/manual/phpfi2.php#switch <https://www.php.net/manual/phpfi2.php#switch> ``` <? $a=0; switch($a) { case 1; echo "a is 1"; break; case "hello"; echo "a is hello"; break; default; echo "a is unknown"; break; }
>
``` Note that every line of code, including `if (...);` and `case...;`, is consistently terminated by a semicolon. In a subsequent major version, the syntax was modified, and you would use a colon instead of a semicolon in a number of places, as you can write today: ``` <?php if ($a==5 && $b!=0): $c = 100 + $a / $b; endif;
>
``` Although the old `if (true);` syntax has been removed (probably because of ambiguity or difficulty of parsing), the old `case 1;` syntax could be left without issue. —Claude

Christoph Becker

6 years ago
On 26.03.2020 at 23:04, Claude Pache wrote:
>> Le 26 mars 2020 à 19:37, Ilija Tovilo <ilija.tovilo@me.com> a écrit : >> >> What's the reasoning behind this? I find it weird an inconsistent. > > This is a manifestly a leftover of an old syntax. Take a look at the PHP/FI 2 manual: > > Language constructs: > https://www.php.net/manual/phpfi2.php#lang <https://www.php.net/manual/phpfi2.php#lang> > > > ``` > <? > if($a==5 && $b!=0 ); > $c = 100 + $a / $b; > endif; > > > ``` > > and, in particular, switch construct: > https://www.php.net/manual/phpfi2.php#switch <https://www.php.net/manual/phpfi2.php#switch> > > ``` > <? > $a=0; > switch($a) { > case 1; > echo "a is 1"; > break; > case "hello"; > echo "a is hello"; > break; > default; > echo "a is unknown"; > break; > } > > > ``` > > Note that every line of code, including `if (...);` and `case...;`, is consistently terminated by a semicolon. > > In a subsequent major version, the syntax was modified, and you would use a colon instead of a semicolon in a number of places, as you can write today: > > ``` > <?php > if ($a==5 && $b!=0): > $c = 100 + $a / $b; > endif; > > > ``` > > Although the old `if (true);` syntax has been removed (probably because of ambiguity or difficulty of parsing), the old `case 1;` syntax could be left without issue.
Interesting! Thanks for digging this out. :)
-- Christoph M. Becker

Andrew Faulds

6 years ago
Hi Ilija, Ilija Tovilo wrote:
> Looking through the language grammer I discovered that switch cases can also be terminated with a `;` instead of a `:`. > > ``` > switch ($i) { > case 1; > return 1; > default; > return 2; > } > ``` > > https://3v4l.org/o7nD8 > > This is in fact documented: > https://www.php.net/manual/en/control-structures.switch.php > > What's the reasoning behind this? I find it weird an inconsistent.
For whatever reason, I somehow remember that this dates to at least PHP/FI 2.0, which means it's as least as old as me (24 years… no, I was not involved with PHP back then ;). And indeed, the case statement is mentioned in the manual (https://www.php.net/manual/phpfi2.php#lang), with an example to be found in https://museum.php.net/php2/php-2.0.tar.gz under php-2.0/test/lang/003.tst: --TEST-- Simple Switch Test ... --POST-- --GET-- --FILE-- <?$a=1; switch($a); case 0; echo "bad"; break; case 1; echo "good"; break; default; echo "bad"; break; endswitch> --EXPECT-- Content-type: text/html good Being able to use a colon instead of a semicolon here is, I guess, a more modern thing. Was it PHP 3 or PHP 4 that changed this? Who knows. I don't think it's hurting anyone as it is… can we leave it in peace? :) Thanks, Andrea