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