# Deprecate alternate switch case syntax?

- Thread: https://externals.io/message/126000
- Original archive: https://news-web.php.net/php.internals/126000
- Started by Theodore Brown on 2024-11-20T14:45:24+00:00
- Messages: 14
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #126000 — Theodore Brown — 2024-11-20T14:45:24+00:00

URL: https://externals.io/message/126000#126000

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

---

## #126001 — Kamil Tekiela — 2024-11-20T15:23:15+00:00

URL: https://externals.io/message/126000#126001 · In reply to: #126000

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

---

## #126002 — Claude Pache — 2024-11-20T16:44:55+00:00

URL: https://externals.io/message/126000#126002 · In reply to: #126000

> 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

---

## #126003 — Larry Garfield — 2024-11-20T17:38:42+00:00

URL: https://externals.io/message/126000#126003 · In reply to: #126002

> > […]
>
> 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

---

## #126004 — Theodore Brown — 2024-11-20T18:26:12+00:00

URL: https://externals.io/message/126000#126004 · In reply to: #126001

> 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

---

## #126006 — Theodore Brown — 2024-11-20T18:45:35+00:00

URL: https://externals.io/message/126000#126006 · In reply to: #126003

> 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

---

## #126007 — Derick Rethans — 2024-11-20T19:03:29+00:00

URL: https://externals.io/message/126000#126007 · In reply to: #126004

>
>
> > […]
>
>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

---

## #126008 — Niels Dossche — 2024-11-20T19:09:14+00:00

URL: https://externals.io/message/126000#126008 · In reply to: #126000

> 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

---

## #126009 — Theodore Brown — 2024-11-20T19:24:32+00:00

URL: https://externals.io/message/126000#126009 · In reply to: #126007

> 
> > […]
>
> 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

---

## #126010 — Derick Rethans — 2024-11-20T19:56:21+00:00

URL: https://externals.io/message/126000#126010 · In reply to: #126009

>
>
> > […]
>
>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

---

## #126016 — tim@bastelstu.be — 2024-11-21T09:36:03+00:00

URL: https://externals.io/message/126000#126016 · In reply to: #126007

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

---

## #126017 — tim@bastelstu.be — 2024-11-21T09:37:33+00:00

URL: https://externals.io/message/126000#126017 · In reply to: #126006

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

---

## #126023 — Christoph M. Becker — 2024-11-21T11:54:16+00:00

URL: https://externals.io/message/126000#126023 · In reply to: #126016

> 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:
>
> > […]

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

---

## #126072 — Theodore Brown — 2024-11-27T20:44:14+00:00

URL: https://externals.io/message/126000#126072 · In reply to: #126017

> Am 2024-11-20 19:45, schrieb Theodore Brown:
> > […]
>
> 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
