Deprecate alternate switch case syntax?

php.internals

Theodore Brown

1 year ago
Hello Internals, I recently found out (after 15 years of being a PHP developer) that PHP allows using a semicolon rather than a colon after case statements. [1] I.e.: switch ($value) { case 'foo'; case 'bar': case 'baz'; echo 'foo, bar, or baz'; break; default; echo 'Other'; } Apparently this syntax has been allowed since at least the PHP 4 days, but very few developers know about it. I ran a script on the top 1000 Composer packages to gauge usage, and out of 35,777 total case statements, only 15 in two packages used the alternate syntax. All were accidental typos (randomly mixed in with statements using the normal syntax), and I opened pull requests to fix both (the first has already been merged): - https://github.com/reactphp/promise/pull/264 - https://www.drupal.org/project/drupal/issues/3486526 Would it be worthwhile for me to write an RFC to propose deprecating the alternate switch case syntax in PHP 8.5? The main reason for deprecation/removal it is that it causes confusion. A developer may think the alternate syntax behaves differently in some way from a regular case statement (e.g. preventing fallthrough or something), when it actually does not. Best regards, Theodore Note: The normal case syntax has been part of the PSR-2 coding style since 2012, and PHP-CS-Fixer automatically fixes usages of the non-standard syntax when using the @PSR2 or newer @PER-CS rulesets. [1]: https://github.com/php/php-src/issues/15258

Kamil Tekiela

1 year ago
Hi Theodore, Do you know why the two exist? Was there any historical reason for this? From what cmb69 said[1], it seems like it may exist because of the implicit semicolon in ?>, and after I tested it, it seems to be true. Was there ever any other reason? [1]: https://github.com/php/php-src/issues/15258#issuecomment-2271809889

Theodore Brown

1 year ago
On Wed, Nov 20, 2024 at 09:23 Kamil Tekiela wrote: > Do you know why the two exist? Was there any historical reason for this? > > From what cmb69 said[1], it seems like it may exist because of the > implicit semicolon in ?>, and after I tested it, it seems to be true. > Was there ever any other reason? Hi Kamil, I hope no one writes code like that where separate <?php ?> tags are interleaved around each switch, case, break, and endswitch statement. That's atrocious... Per the previous discussion that Claude linked to, the alternate syntax is a leftover from PHP/FI 2, where nearly all lines including if conditions and case statements were terminated by a semicolon. [2] [1]: https://github.com/php/php-src/issues/15258#issuecomment-2271809889 [2]: https://externals.io/message/109350#109363

Derick Rethans

1 year ago
On 20 November 2024 19:26:12 CET, Theodore Brown <theodorejb@outlook.com> wrote:
>On Wed, Nov 20, 2024 at 09:23 Kamil Tekiela wrote: > >> Do you know why the two exist? Was there any historical reason for this? >> >> From what cmb69 said[1], it seems like it may exist because of the >> implicit semicolon in ?>, and after I tested it, it seems to be true. >> Was there ever any other reason? > >I hope no one writes code like that where separate <?php ?> tags are interleaved around each switch, case, break, and endswitch statement. That's atrocious...
All my PHP based templates for the xdebug.org site use this style. I don't think it's atrocious, and quite a bit nicer than the "new" syntax. It's also not a decades old choice (and not even by me). cheers Derick

Theodore Brown

1 year ago
On Wed, Nov. 20, 2024 at 13:03 Derick Rethans wrote: > On 20 November 2024 19:26:12 CET, Theodore Brown wrote: >> On Wed, Nov 20, 2024 at 09:23 Kamil Tekiela wrote: >> >>> Do you know why the two exist? Was there any historical reason for this? >>> >>> From what cmb69 said[1], it seems like it may exist because of the >>> implicit semicolon in ?>, and after I tested it, it seems to be true. >>> Was there ever any other reason? >> >> I hope no one writes code like that where separate <?php ?> tags are interleaved around each switch, case, break, and endswitch statement. That's atrocious... > > All my PHP based templates for the xdebug.org site use this style. I don't think it's atrocious, and quite a bit nicer than the "new" syntax. > > It's also not a decades old choice (and not even by me). Hi Derick, I suppose whether or not that approach is ugly is somewhat subjective. Do the xdebug.org templates using this style also rely on case statements being followed by a semicolon instead of a colon? Sincerely, Theodore

Derick Rethans

1 year ago
On 20 November 2024 20:24:32 CET, Theodore Brown <theodorejb@outlook.com> wrote:
>On Wed, Nov. 20, 2024 at 13:03 Derick Rethans wrote: > >> On 20 November 2024 19:26:12 CET, Theodore Brown wrote: >>> On Wed, Nov 20, 2024 at 09:23 Kamil Tekiela wrote: >>> >>>> Do you know why the two exist? Was there any historical reason for this? >>>> >>>> From what cmb69 said[1], it seems like it may exist because of the >>>> implicit semicolon in ?>, and after I tested it, it seems to be true. >>>> Was there ever any other reason? >>> >>> I hope no one writes code like that where separate <?php ?> tags are interleaved around each switch, case, break, and endswitch statement. That's atrocious... >> >> All my PHP based templates for the xdebug.org site use this style. I don't think it's atrocious, and quite a bit nicer than the "new" syntax. >> >> It's also not a decades old choice (and not even by me). > >Hi Derick, > >I suppose whether or not that approach is ugly is somewhat subjective. Do the xdebug.org templates using this style also rely on case statements being followed by a semicolon instead of a colon? > >Sincerely, >Theodore
Even if it did, it's not really relevant for the discussion. I was mentioning this because removing the syntax would break code for no real gain. Cheers Derick

Tim Düsterhus

1 year ago
Hi Am 2024-11-20 20:03, schrieb Derick Rethans:
> All my PHP based templates for the xdebug.org site use this style. I > don't think it's atrocious, and quite a bit nicer than the "new" > syntax.
This proposal would not affect that. You would just need to add a colon after the `case` statement, not a semicolon. In fact that would more closely match the corresponding `switch`, which would also need to have a colon: <?php $foo = "bar"; ?> <?php switch ($foo): case "bar": ?> BAR <?php break; case "baz": ?> BAZ <?php endswitch; ?> Though using `switch` in such templates is somewhat finicky anyway, because it will need to be merged with the first `case`, because otherwise:
> Parse error: syntax error, unexpected T_INLINE_HTML "", expecting > "endswitch" or "case" or "default"
Best regards Tim Düsterhus

Christoph Becker

1 year ago
On 21.11.2024 at 10:36, Tim Düsterhus wrote:
> This proposal would not affect that. You would just need to add a colon > after the `case` statement, not a semicolon. In fact that would more > closely match the corresponding `switch`, which would also need to have > a colon: > >     <?php $foo = "bar"; ?> > >     <?php >     switch ($foo): >     case "bar": >     ?> >     BAR >     <?php break; case "baz": ?> >     BAZ >     <?php endswitch; ?> > > Though using `switch` in such templates is somewhat finicky anyway, > because it will need to be merged with the first `case`, because otherwise: > >> Parse error: syntax error, unexpected T_INLINE_HTML "", expecting >> "endswitch" or "case" or "default"
There is no parse error if you use PHP snippets in such templates like you use C preprocessor directives (i.e. always starting at the first column); e.g. https://3v4l.org/cK0Ll. Or see https://3v4l.org/7mKS8, which is how I would write it, if I was using switch statements in such templates. Note that I'm not advocating to keep the behavior, but I also don't see how it would be harmful. Without an enforced CS, there can be more confusing code anyway. Christoph

Claude Pache

1 year ago
> Le 20 nov. 2024 à 15:45, Theodore Brown <theodorejb@outlook.com> a écrit : > > Hello Internals, > > I recently found out (after 15 years of being a PHP developer) that PHP allows using a semicolon rather than a colon after case statements. [1] I.e.: > >
Hi, For reference, previous discussion on the subject: https://externals.io/message/109350 —Claude

Larry Garfield

1 year ago
On Wed, Nov 20, 2024, at 10:44 AM, Claude Pache wrote:
>> Le 20 nov. 2024 à 15:45, Theodore Brown <theodorejb@outlook.com> a écrit : >> >> Hello Internals, >> >> I recently found out (after 15 years of being a PHP developer) that PHP allows using a semicolon rather than a colon after case statements. [1] I.e.: >> >> > > Hi, > > For reference, previous discussion on the subject: > > https://externals.io/message/109350 > > —Claude
Interesting history! Chesterton's Fence gate achieved. :-) Unless it's causing parser issues somewhere I don't think we *need* to address it. Maybe just document that one really shouldn't use it, and it's only there for legacy reasons. That said, if someone wants to go to the work of removing it, I'd probably support it. --Larry Garfield

Theodore Brown

1 year ago
On Wed, Nov. 20, 2024 at 11:38 Larry Garfield wrote: > Unless it's causing parser issues somewhere I don't think we *need* to address it. Maybe just document that one really shouldn't use it, and it's only there for legacy reasons. That said, if someone wants to go to the work of removing it, I'd probably support it. I don't feel particularly strongly about it, but I am inclined to write an RFC to deprecate it, since removing some of these little-known legacy syntaxes makes the language cleaner and less confusing, and sometimes can even pave the way for future features. For example, the curly brace syntax for accessing array elements and string offsets was deprecated in PHP 7.4,[1] and if I recall this made possible the property hooks syntax we have now in PHP 8.4. Sincerely, Theodore [1]: https://wiki.php.net/rfc/deprecate_curly_braces_array_access

Tim Düsterhus

1 year ago
Hi Am 2024-11-20 19:45, schrieb Theodore Brown:
> I don't feel particularly strongly about it, but I am inclined to write > an RFC to deprecate it
It doesn't feel big enough to warrant its own RFC, but I would be happy to see it as part of https://wiki.php.net/rfc/deprecations_php_8_5. Best regards Tim Düsterhus

Theodore Brown

1 year ago
On Thu, Nov. 21, 2024 at 03:37 Tim Düsterhus wrote: > Am 2024-11-20 19:45, schrieb Theodore Brown: >> I don't feel particularly strongly about it, but I am inclined to write an RFC to deprecate it > > It doesn't feel big enough to warrant its own RFC, > but I would be happy to see it as part of https://wiki.php.net/rfc/deprecations_php_8_5. > > Best regards > Tim Düsterhus Hi Tim, I have now added an item to the general deprecations RFC for PHP 8.5 to deprecate terminating case statements with a semicolon. Kind regards, Theodore Brown

Niels Dossche

1 year ago
On 20/11/2024 15:45, Theodore Brown wrote:
> Hello Internals, > > I recently found out (after 15 years of being a PHP developer) that PHP allows using a semicolon rather than a colon after case statements. [1] I.e.: > > switch ($value) { > case 'foo'; > case 'bar': > case 'baz'; > echo 'foo, bar, or baz'; > break; > default; > echo 'Other'; > } > > Apparently this syntax has been allowed since at least the PHP 4 days, but very few developers know about it. > > I ran a script on the top 1000 Composer packages to gauge usage, and out of 35,777 total case statements, only 15 in two packages used the alternate syntax. All were accidental typos (randomly mixed in with statements using the normal syntax), and I opened pull requests to fix both (the first has already been merged): > > - https://github.com/reactphp/promise/pull/264 > - https://www.drupal.org/project/drupal/issues/3486526 > > Would it be worthwhile for me to write an RFC to propose deprecating the alternate switch case syntax in PHP 8.5? > > The main reason for deprecation/removal it is that it causes confusion. A developer may think the alternate syntax behaves differently in some way from a regular case statement (e.g. preventing fallthrough or something), when it actually does not. > > Best regards, > Theodore > > Note: The normal case syntax has been part of the PSR-2 coding style since 2012, and PHP-CS-Fixer automatically fixes usages of the non-standard syntax when using the @PSR2 or newer @PER-CS rulesets. > > [1]: https://github.com/php/php-src/issues/15258
Hi I'm not convinced that getting rid of this is a good idea. It doesn't seem to cause real harm and as Kamil/cmb pointed out there was a legit reason to have this. So I'm not in favor of deprecating this. Kind regards Niels