# semicolon terminator for switch cases

- Thread: https://externals.io/message/109350
- Original archive: https://news-web.php.net/php.internals/109350
- Started by Ilija Tovilo on 2020-03-26T18:37:07+00:00
- Messages: 10
- RFC: Deprecations for PHP 8.5 (Implemented (PHP 8.5)) — https://externals.io/rfc/deprecations_php_8_5.md
- 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

---

## #109350 — Ilija Tovilo — 2020-03-26T18:37:07+00:00

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

Hi internals

Looking through the language grammer I discovered that switch cases can also be terminated with a `;` instead of a `:`.

```
switch ($i) {
    case 1; 
        return 1;
    default; 
        return 2;
}
```

https://3v4l.org/o7nD8

This is in fact documented:
https://www.php.net/manual/en/control-structures.switch.php

What's the reasoning behind this? I find it weird an inconsistent.

Maybe something to deprecate in PHP 8.0.
https://wiki.php.net/rfc/deprecations_php_8_0

Regards

---

## #109356 — Stanislav Malyshev — 2020-03-26T20:14:14+00:00

URL: https://externals.io/message/109350#109356 · In reply to: #109350

Hi!

> Maybe something to deprecate in PHP 8.0.
> https://wiki.php.net/rfc/deprecations_php_8_0

Why? Whose life it'd make easier?

-- 
Stas Malyshev
smalyshev@gmail.com

---

## #109357 — David Rodrigues — 2020-03-26T20:26:13+00:00

URL: https://externals.io/message/109350#109357 · In reply to: #109356

I agree with Tovilo. It is weird and confusing. Actually I never know that
it was possible. And the ";" sounds like "it ends here", while ":" counds
like "it does it ->". For me, "case 1;" will sounds like "skip case 1".


Atenciosamente,
David Rodrigues


Em qui., 26 de mar. de 2020 às 17:14, Stanislav Malyshev <
smalyshev@gmail.com> escreveu:

> Hi!
>
> > […]
>
> Why? Whose life it'd make easier?
>
> --
> Stas Malyshev
> smalyshev@gmail.com

---

## #109358 — Stanislav Malyshev — 2020-03-26T20:28:38+00:00

URL: https://externals.io/message/109350#109358 · In reply to: #109357

Hi!


> I agree with Tovilo. It is weird and confusing. Actually I never know
> that it was possible. 

You seem to contradict yourself - if you never knew it was possible, how
could you ever be confused by it?

-- 
Stas Malyshev
smalyshev@gmail.com

---

## #109359 — Benoit SCHILDKNECHT — 2020-03-26T20:49:30+00:00

URL: https://externals.io/message/109350#109359 · In reply to: #109358

Le Thu, 26 Mar 2020 21:28:38 +0100, Stanislav Malyshev  
<smalyshev@gmail.com> a écrit:

> Hi!
>
> 
> > […]
>
> You seem to contradict yourself - if you never knew it was possible, how
> could you ever be confused by it?
>

OK, I'm often lurking, but I have to react to this. I never knew it was  
possible too, but I am also confused by this. A semi-colon is terminal. It  
always is. "You see this set of instructions? Done.". But in this case, it  
is not. You see the problem?

-- 
L'absence de virus dans ce courrier électronique a été vérifiée par le logiciel antivirus Avast.
https://www.avast.com/antivirus

---

## #109362 — Claude Pache — 2020-03-26T21:30:22+00:00

URL: https://externals.io/message/109350#109362 · In reply to: #109359

> Le 26 mars 2020 à 21:49, Benoit SCHILDKNECHT <bensor987@neuf.fr> a écrit :
> 
> A semi-colon is terminal. Italways is. "You see this set of instructions? Done.". But in this case, itis not. You see the problem?
> 

No I don’t see the problem. Inside  a switch construct, when you reach the end of an instruction (or of a set of instructions) that is not (resp. does not end with) a control-flow statement, you pass to the next one. I’m sure that everybody knows that you have to use an explicit control-flow statement like “break” or “return” if you want to be “done” before reaching the bottom of the switch construct.

Or maybe people could misguess that “;” in that context is an odd way to mean “: break;”? 

—Claude

---

## #109363 — Claude Pache — 2020-03-26T22:04:34+00:00

URL: https://externals.io/message/109350#109363 · In reply to: #109350

> Le 26 mars 2020 à 19:37, Ilija Tovilo <ilija.tovilo@me.com> a écrit :
> 
> What's the reasoning behind this? I find it weird an inconsistent.
> 

This is a manifestly a leftover of an old syntax. Take a look at the PHP/FI 2 manual:

Language constructs:
https://www.php.net/manual/phpfi2.php#lang <https://www.php.net/manual/phpfi2.php#lang>


```
    <?
      if($a==5 &&  $b!=0 );
        $c = 100 + $a / $b;
      endif;
    >
```

and, in particular, switch construct:
https://www.php.net/manual/phpfi2.php#switch <https://www.php.net/manual/phpfi2.php#switch>

```
      <?
          $a=0;
          switch($a) {
                case 1;
                  echo "a is 1";
                  break;
                case "hello";
                  echo "a is hello";
                  break;
                default;
                  echo "a is unknown";
                  break;
          }
        >
```

Note that every line of code, including `if (...);` and `case...;`, is consistently terminated by a semicolon.

In a subsequent major version, the syntax was modified, and you would use a colon instead of a semicolon in a number of places, as you can write today:

```
    <?php
      if ($a==5 &&  $b!=0):
        $c = 100 + $a / $b;
      endif;
    >
```

Although the old `if (true);` syntax has been removed (probably because of ambiguity or difficulty of parsing), the old `case 1;` syntax could be left without issue.

—Claude

---

## #109364 — Andrea Faulds — 2020-03-26T22:07:40+00:00

URL: https://externals.io/message/109350#109364 · In reply to: #109350

Hi Ilija,

Ilija Tovilo wrote:
> Looking through the language grammer I discovered that switch cases can also be terminated with a `;` instead of a `:`.
> 
> ```
> switch ($i) {
>      case 1;
>          return 1;
>      default;
>          return 2;
> }
> ```
> 
> https://3v4l.org/o7nD8
> 
> This is in fact documented:
> https://www.php.net/manual/en/control-structures.switch.php
> 
> What's the reasoning behind this? I find it weird an inconsistent.

For whatever reason, I somehow remember that this dates to at least 
PHP/FI 2.0, which means it's as least as old as me (24 years… no, I was 
not involved with PHP back then ;). And indeed, the case statement is 
mentioned in the manual (https://www.php.net/manual/phpfi2.php#lang), 
with an example to be found in 
https://museum.php.net/php2/php-2.0.tar.gz under php-2.0/test/lang/003.tst:

     --TEST--
     Simple Switch Test                                            ...
     --POST--
     --GET--
     --FILE--
     <?$a=1;
       switch($a);
         case 0;
             echo "bad";
             break;
         case 1;
             echo "good";
             break;
         default;
             echo "bad";
             break;
       endswitch>
     --EXPECT--
     Content-type: text/html
     good

Being able to use a colon instead of a semicolon here is, I guess, a 
more modern thing. Was it PHP 3 or PHP 4 that changed this? Who knows.

I don't think it's hurting anyone as it is… can we leave it in peace? :)

Thanks,
Andrea

---

## #109365 — Christoph M. Becker — 2020-03-26T22:38:43+00:00

URL: https://externals.io/message/109350#109365 · In reply to: #109363

> > […]
>
> This is a manifestly a leftover of an old syntax. Take a look at the PHP/FI 2 manual:
>
> Language constructs:
> https://www.php.net/manual/phpfi2.php#lang <https://www.php.net/manual/phpfi2.php#lang>
>
>
> ```
>     <?
>       if($a==5 &&  $b!=0 );
>         $c = 100 + $a / $b;
>       endif;
> > […]
> ```
>
> and, in particular, switch construct:
> https://www.php.net/manual/phpfi2.php#switch <https://www.php.net/manual/phpfi2.php#switch>
>
> ```
>       <?
>           $a=0;
>           switch($a) {
>                 case 1;
>                   echo "a is 1";
>                   break;
>                 case "hello";
>                   echo "a is hello";
>                   break;
>                 default;
>                   echo "a is unknown";
>                   break;
>           }
> > […]
> ```
>
> Note that every line of code, including `if (...);` and `case...;`, is consistently terminated by a semicolon.
>
> In a subsequent major version, the syntax was modified, and you would use a colon instead of a semicolon in a number of places, as you can write today:
>
> ```
>     <?php
>       if ($a==5 &&  $b!=0):
>         $c = 100 + $a / $b;
>       endif;
> > […]
> ```
>
> Although the old `if (true);` syntax has been removed (probably because of ambiguity or difficulty of parsing), the old `case 1;` syntax could be left without issue.

Interesting!  Thanks for digging this out. :)

--
Christoph M. Becker

---

## #109370 — Ilija Tovilo — 2020-03-27T11:01:19+00:00

URL: https://externals.io/message/109350#109370

Hi Andrea

The reason this doesn't work anymore:

```
If (false);
    echo 'Test';
endif;
```

Is because the semicolon is an empty statement:
https://github.com/php/php-src/blob/9e77d5a9da9e80a1bfe226f60ccb12fd8bf9481c/Zend/zend_language_parser.y#L457

This is still valid code:

```
If (false);
    echo 'Test';
```

And it always prints. The two semicolons look the same but behave differently which is why I'm in favor of deprecating it.

Regards
