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):
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.
Hi Theodore,
Do you know why the two exist? Was there any historical reason for this?
From what cmb69 said1, 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?
Do you know why the two exist? Was there any historical reason for this?
From what cmb69 said1, 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
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
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
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
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
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
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
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):
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,
TheodoreNote: 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.
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