Re: Deprecate alternate switch case syntax?

From: Date: Wed, 20 Nov 2024 19:09:14 +0000
Subject: Re: Deprecate alternate switch case syntax?
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-126008@lists.php.net to get a copy of this message
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

« previous php.internals (#126008) next »