# [RFC] Deprecate and remove continue targeting switch

- Thread: https://externals.io/message/102393
- Original archive: https://news-web.php.net/php.internals/102393
- Started by Nikita Popov on 2018-06-24T16:16:19+00:00
- Messages: 27
- RFC: Deprecate and remove continue targeting switch (Withdrawn) — https://externals.io/rfc/continue_on_switch_deprecation.md
- Page 1 of 2: messages 1 to 25 (25 per page, add `?page=N` to the URL)
- Next page: https://externals.io/message/102393.md?page=2
- 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

---

## #102393 — Nikita Popov — 2018-06-24T16:16:19+00:00

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

Hi internals,

Another small deprecation for your consideration...

https://wiki.php.net/rfc/continue_on_switch_deprecation

Regards,
Nikita

---

## #102408 — Stanislav Malyshev — 2018-06-25T05:53:45+00:00

URL: https://externals.io/message/102393#102408 · In reply to: #102393

Hi!


> Hi internals,
> 
> Another small deprecation for your consideration...
> 
> https://wiki.php.net/rfc/continue_on_switch_deprecation

Not sure I understand - what this is improving? Yes, continue being the
same as break is slightly surprising, but it doesn't seem to hurt
anything and I don't think it happens too often, so why change it at all?

-- 
Stas Malyshev
smalyshev@gmail.com

---

## #102461 — Nikita Popov — 2018-06-26T18:27:40+00:00

URL: https://externals.io/message/102393#102461 · In reply to: #102408

On Mon, Jun 25, 2018 at 7:53 AM, Stanislav Malyshev <smalyshev@gmail.com>
wrote:

> Hi!
>
> 
> > […]
>
> Not sure I understand - what this is improving? Yes, continue being the
> same as break is slightly surprising, but it doesn't seem to hurt
> anything and I don't think it happens too often, so why change it at all?
>

This RFC resolves the following issue:

a) In PHP "switch" is considered a looping structure, for this reason
"break" and "continue" both apply to "switch", as aliases. For PHP, these
are reasonable semantics, as PHP supports multi-level breaks. It would be
very questionable if "break N" and "continue N" could refer to different
loop structures just because there is a "switch" involved somewhere.

b) On the other hand, other languages, such as C and C-based languages, use
a different behavior, where "continue" refers to the enclosing loop. As
these languages don't have multi-level breaks, these semantics are also
reasonable.

This leaves us in a position where "continue" means different things in PHP
and in other syntactically similar languages, causing confusing for
programmers familiar with them. This is solved by simply prohibiting the
case that is ambiguous. This is not a loss for PHP developers (who use
"break" instead of "continue" for switch statements anyway), but it is a
gain for programmers from other languages not familiar with this detail (as
they get an error pointing them to one of the correct alternatives).

All of this is not a big issue, it's just removing a papercut. IIRC this
was originally motivated by me seeing some bug reports about this behavior.

Nikita

---

## #102463 — Stanislav Malyshev — 2018-06-26T19:04:29+00:00

URL: https://externals.io/message/102393#102463 · In reply to: #102461

Hi!

> This leaves us in a position where "continue" means different things in
> PHP and in other syntactically similar languages, causing confusing for
> programmers familiar with them. This is solved by simply prohibiting the
> case that is ambiguous. This is not a loss for PHP developers (who use

I see your point, in this case I'd add a notice for continue in switch
that would say something along the lines "continue in switch is the same
as break, did you mean "continue 2;"?"
-- 
Stas Malyshev
smalyshev@gmail.com

---

## #102464 — niel — 2018-06-26T19:28:26+00:00

URL: https://externals.io/message/102393#102464 · In reply to: #102393

> Hi internals,
> 
> Another small deprecation for your consideration...
> 
> https://wiki.php.net/rfc/continue_on_switch_deprecation
> 
> Regards,
> Nikita
> 
IMO the proposed illustrations will lead to as much, if not more, WTF 
from other language users as current behaviour. Why? Because the 
programmer still has to account for the 'switch' in how continue works. 
This will just cause a BC problem for older code without adding anything 
for people new to PHP.

My suggestion, is to make the break (no pun intended) be clean. 
Deprecate use of continue from usage to escape switch completely in next 
PHP, then remove that functionality in PHP 8.

---

## #102471 — Rowan Collins — 2018-06-26T20:49:29+00:00

URL: https://externals.io/message/102393#102471 · In reply to: #102464

> 
> > […]
> IMO the proposed illustrations will lead to as much, if not more, WTF 
> from other language users as current behaviour. Why? Because the 
> programmer still has to account for the 'switch' in how continue 
> works. This will just cause a BC problem for older code without adding 
> anything for people new to PHP.
>
> My suggestion, is to make the break (no pun intended) be clean. 
> Deprecate use of continue from usage to escape switch completely in 
> next PHP, then remove that functionality in PHP 8.
>

Do you mean that this code would be valid in both PHP 7 and PHP 8, but 
mean different things?

while($outer) {
     while($inner) {
         switch($foo) {
             case 1:
                 continue 2;
         }
     }
}

If so, I think that would be even more confusing.

Regards,

-- 
Rowan Collins
[IMSoP]

---

## #102472 — Stanislav Malyshev — 2018-06-26T22:44:09+00:00

URL: https://externals.io/message/102393#102472 · In reply to: #102471

Hi!

> Do you mean that this code would be valid in both PHP 7 and PHP 8, but
> mean different things?
> 
> while($outer) {
>     while($inner) {
>         switch($foo) {
>             case 1:
>                 continue 2;
>         }
>     }
> }
> 

I think this would be unacceptable, because WTF potential and subtle
code breakage that would result would be huge. If there are several
wrapped loops, it'd be almost impossible to detect that you're now
breaking a wrong loop. I think the only thing that could be changed is
"continue;" - without an argument - and the thing that should happen is
a notice that it may not do what you (might) think it's doing. If you
use something like "continue 2;" I would assume you know what you're
doing, this is a PHP-specific construct so it is reasonable to assume
you checked it does what you think it does.

-- 
Stas Malyshev
smalyshev@gmail.com

---

## #102478 — Zeev Suraski — 2018-06-27T06:00:36+00:00

URL: https://externals.io/message/102393#102478 · In reply to: #102461

> -----Original Message-----
> From: Nikita Popov [mailto:nikita.ppv@gmail.com]
> Sent: Tuesday, June 26, 2018 9:28 PM
> To: Stanislav Malyshev <smalyshev@gmail.com>
> Cc: PHP internals <internals@lists.php.net>
> Subject: Re: [PHP-DEV] [RFC] Deprecate and remove continue targeting switch
> 
> This RFC resolves the following issue:
> 
> a) In PHP "switch" is considered a looping structure, for this reason "break" and
> "continue" both apply to "switch", as aliases. For PHP, these are reasonable
> semantics, as PHP supports multi-level breaks. It would be very questionable if
> "break N" and "continue N" could refer to different loop structures just because
> there is a "switch" involved somewhere.
> 
> b) On the other hand, other languages, such as C and C-based languages, use a
> different behavior, where "continue" refers to the enclosing loop. As these
> languages don't have multi-level breaks, these semantics are also reasonable.
> 
> This leaves us in a position where "continue" means different things in PHP and
> in other syntactically similar languages, causing confusing for programmers
> familiar with them. This is solved by simply prohibiting the case that is
> ambiguous. This is not a loss for PHP developers (who use "break" instead of
> "continue" for switch statements anyway), but it is a gain for programmers from
> other languages not familiar with this detail (as they get an error pointing them
> to one of the correct alternatives).
> 
> All of this is not a big issue, it's just removing a papercut. IIRC this was originally
> motivated by me seeing some bug reports about this behavior.

I think it would be helpful to gauge the levels of these bug reports to understand whether it's worth the trouble.

Ultimately - even after this change, C developers won't be able to use the C semantics they're used to, as (e.g. in the example in the RFC) - 'continue;' won't work as they'd expect - and they'd have to acquaint themselves with 'continue 2;'.  It's true that if we 'force' them to acquaint themselves with multi-level loop control structures there's reduced 'continue;' erroneously.  But unless this is really A Thing, as in, something pretty common - I think the mess involved with forcing people who have working code to try and (a) understand the change, and (b) understand their (potentially quite ancient) code flows may outweigh the benefits.

Zeev

---

## #102487 — niel — 2018-06-27T15:14:42+00:00

URL: https://externals.io/message/102393#102487 · In reply to: #102471

> 
> > […]
> 
> Do you mean that this code would be valid in both PHP 7 and PHP 8, but 
> mean different things?

How is that different to the illustrations provided? That code is valid 
in PHP 7.0-7.3 but will have different meaning in 7.3.

> while($outer) {
>      while($inner) {
>          switch($foo) {
>              case 1:
>                  continue 2;
>          }
>      }
> }
> 
> If so, I think that would be even more confusing.
> 
> Regards,
> 

Ultimately, I was trying to express what Zeev said better:

> Ultimately - even after this change, C developers won't be able to use the C semantics they're used to, as (e.g. in the example in the RFC) - 'continue;' won't work as they'd expect - and they'd have to acquaint themselves with 'continue 2;'.  It's true that if we 'force' them to acquaint themselves with multi-level loop control structures there's reduced 'continue;' erroneously.  But unless this is really A Thing, as in, something pretty common - I think the mess involved with forcing people who have working code to try and (a) understand the change, and (b) understand their (potentially quite ancient) code flows may outweigh the benefits.

---

## #102490 — Rowan Collins — 2018-06-27T15:33:19+00:00

URL: https://externals.io/message/102393#102490 · In reply to: #102487

> 
>
> > […]
>
> How is that different to the illustrations provided? That code is valid in
> PHP 7.0-7.3 but will have different meaning in 7.3.
>


In the proposal, the code is valid in all versions of PHP up to and
including 7.3, *and represents the same algorithm in all versions*. The
only difference would be the side-effect of issuing an additional
deprecation notice; that's not "a different meaning". In PHP 8, under
Nikita's proposal, the code would not be valid at all.

The RFC reiterates this under "Unaffected PHP Functionality":

> The meaning of continue and break inside switch never changes, some cases
are just forbidden.

In contrast, if we make the continue that previously referenced the switch
now reference the loop, the same code will work in PHP 8, but now
*represent an entirely different algorithm*.



>
> Ultimately, I was trying to express what Zeev said better:
>
> Ultimately - even after this change, C developers won't be able to use the
> > […]
>
>

If we change the behaviour, rather than removing it, we solve the first
problem (C developers will get the semantics they're used to) but we will
make the second problem worse (people with working code will need to
understand why it now does something different).

Regards,
-- 
Rowan Collins
[IMSoP]

---

## #102491 — niel — 2018-06-27T15:45:40+00:00

URL: https://externals.io/message/102393#102491 · In reply to: #102393

> Hi internals,
> 
> Another small deprecation for your consideration...
> 
> https://wiki.php.net/rfc/continue_on_switch_deprecation
> 
> Regards,
> Nikita
> 

Could you clarify the PHP 8 changes:

"PHP 8 generates a compile error."

Does this mean *any* use of continue in a switch is a compile error, or 
only continue without a number

---

## #102493 — Chase Peeler — 2018-06-27T15:53:29+00:00

URL: https://externals.io/message/102393#102493 · In reply to: #102491

> 
> > […]
>
> Could you clarify the PHP 8 changes:
>
> "PHP 8 generates a compile error."
>
> Does this mean *any* use of continue in a switch is a compile error, or
> only continue without a number
>
> --
> 
> 
>
> Something like this could make upgrading PHP against a large legacy code
base near impossible.

If this was the ONLY thing that a developer in another language would have
to grapple with in order to switch the PHP, it might be different. But, I
would advise against potentially breaking a lot of code for something that
provides no functional advantage, and, at best, might provide a slight
syntactical improvement (and, I'm not convinced it even does that).
-- 
-- Chase
chasepeeler@gmail.com

---

## #102494 — Rowan Collins — 2018-06-27T15:59:02+00:00

URL: https://externals.io/message/102393#102494 · In reply to: #102491

> 
>
> > […]
> Could you clarify the PHP 8 changes:
>
> "PHP 8 generates a compile error."
>
> Does this mean *any* use of continue in a switch is a compile error, or
> only continue without a number
>


Neither:

> Continue can still be used inside switch statements, as long as it does
not target the switch.

The lines marked "Deprecated" in the examples are the ones which will be
compile errors.

Regards,
-- 
Rowan Collins
[IMSoP]

---

## #102495 — Nikita Popov — 2018-06-27T16:08:16+00:00

URL: https://externals.io/message/102393#102495 · In reply to: #102494

On Wed, Jun 27, 2018 at 5:59 PM, Rowan Collins <rowan.collins@gmail.com>
wrote:

> 
>
> > […]
>
>
> Neither:
>
> > […]
> not target the switch.
>
> The lines marked "Deprecated" in the examples are the ones which will be
> compile errors.
>

Thank you Rowan. I have added a code example to
https://wiki.php.net/rfc/continue_on_switch_deprecation#unaffected_php_functionality
to further clarify what is deprecated and what continues to be allowed.

Nikita

---

## #102498 — niel — 2018-06-27T16:35:04+00:00

URL: https://externals.io/message/102393#102498 · In reply to: #102494

> 
> 
> > […]
> 
> 
> Neither:
> 
> > […]
> not target the switch

> The lines marked "Deprecated" in the examples are the ones which will be
> compile errors.

This appears to contradict your 'neither'. `continue` as part of a 
`case` will always be a compile error in 8? Apologies if my mention of 
switch previously was ambiguous.

Will the PHP 8 version still require the programmer to count over the 
switches, i.e. in the third illustration, would the continue 3 still be 
needed for C-like behaviour?
If that is true, then I really have no use for this proposal as it 
doesn't fix the problem, just moves it.

---

## #102499 — niel — 2018-06-27T16:42:55+00:00

URL: https://externals.io/message/102393#102499 · In reply to: #102495

> On Wed, Jun 27, 2018 at 5:59 PM, Rowan Collins <rowan.collins@gmail.com
> <mailto:rowan.collins@gmail.com>> wrote:
> 
>      On 27 June 2018 at 16:45, niel <spam-free@blueyonder.co.uk
>      <mailto:spam-free@blueyonder.co.uk>> wrote:
> 
> > […]
>      <https://wiki.php.net/rfc/continue_on_switch_deprecation>
> > […]
> 
> 
>      Neither:
> 
> > […]
>      not target the switch.
> 
>      The lines marked "Deprecated" in the examples are the ones which will be
>      compile errors.
> 
> 
> Thank you Rowan. I have added a code example to
> https://wiki.php.net/rfc/continue_on_switch_deprecation#unaffected_php_functionality
> to further clarify what is deprecated and what continues to be allowed.
> 
> Nikita
> 
Thanks, this makes it clearer.

---

## #102500 — Rowan Collins — 2018-06-27T17:43:39+00:00

URL: https://externals.io/message/102393#102500 · In reply to: #102461

> a) In PHP "switch" is considered a looping structure, for this reason
> "break" and "continue" both apply to "switch", as aliases. For PHP, these
> are reasonable semantics, as PHP supports multi-level breaks. It would be
> very questionable if "break N" and "continue N" could refer to different
> loop structures just because there is a "switch" involved somewhere.
>


I think this point should be highlighted: this is not just an unfortunate
legacy decision, it would be *really bad* if "break 2;" sometimes referred
to a different loop from "continue 2;"

Other languages don't have that problem, because they don't have the
"continue N" syntax.

I'm +0.5 on Nikita's proposal to make this an error - it can't mean
anything other than "continue with this switch statement", but that doesn't
actually mean anything. The error is effectively saying "Can't 'continue' a
switch, because it's not really a loop."

Perhaps rather than deprecating and removing it, we should simply raise a
Warning on its use: "Use of 'continue' to terminate switch; did you mean
'break', or to continue a loop instead?" In other words, "we've done what
you asked, but we think it might not be what you meant".

Regards,
-- 
Rowan Collins
[IMSoP]

---

## #102505 — Stanislav Malyshev — 2018-06-27T21:07:54+00:00

URL: https://externals.io/message/102393#102505 · In reply to: #102490

Hi!

> In the proposal, the code is valid in all versions of PHP up to and
> including 7.3, *and represents the same algorithm in all versions*. The
> only difference would be the side-effect of issuing an additional
> deprecation notice; that's not "a different meaning". In PHP 8, under
> Nikita's proposal, the code would not be valid at all.

I agree with PHP 7 part but not with PHP 8 part. I think we should just
leave the warning there.

-- 
Stas Malyshev
smalyshev@gmail.com

---

## #102574 — Nikita Popov — 2018-07-04T14:14:56+00:00

URL: https://externals.io/message/102393#102574 · In reply to: #102505

On Wed, Jun 27, 2018 at 11:07 PM, Stanislav Malyshev <smalyshev@gmail.com>
wrote:

> Hi!
>
> > […]
>
> I agree with PHP 7 part but not with PHP 8 part. I think we should just
> leave the warning there.
>

I don't care particularly strongly about the actual removal, just having a
warning would serve the purpose just as well for me.

Should I just go ahead and implement the warning (with no intent of future
removal), or do we want to still go through the RFC here?

Nikita

---

## #102575 — Zeev Suraski — 2018-07-04T14:51:30+00:00

URL: https://externals.io/message/102393#102575 · In reply to: #102574

> On Wed, Jun 27, 2018 at 11:07 PM, Stanislav Malyshev <smalyshev@gmail.com>
> wrote:
>
> > […]
>
> I don't care particularly strongly about the actual removal, just having a
> warning would serve the purpose just as well for me.
>
> Should I just go ahead and implement the warning (with no intent of future
> removal), or do we want to still go through the RFC here?
>

I think a sensible warning is fine and doesn't really require an RFC...

Zeev

---

## #102578 — Christoph M. Becker — 2018-07-04T15:28:40+00:00

URL: https://externals.io/message/102393#102578 · In reply to: #102575

> 
> 
> > […]
>
> I think a sensible warning is fine and doesn't really require an RFC...

Would be fine for me as well.

-- 
Christoph M. Becker

---

## #102581 — Nikita Popov — 2018-07-04T17:16:30+00:00

URL: https://externals.io/message/102393#102581 · In reply to: #102578

On Wed, Jul 4, 2018 at 5:28 PM, Christoph M. Becker <cmbecker69@gmx.de>
wrote:

> 
>
> > […]
> wrote:
> > […]
> smalyshev@gmail.com>
> > […]
> The
> > […]
> having a
> > […]
> future
> > […]
>
> Would be fine for me as well.
>

Okay, thanks for the feedback. I've implemented the warning in
https://github.com/php/php-src/pull/3364 and will merge the PR in a few
days if there are no further objections.

Nikita

---

## #102585 — Stanislav Malyshev — 2018-07-04T17:39:05+00:00

URL: https://externals.io/message/102393#102585 · In reply to: #102574

Hi!

> Should I just go ahead and implement the warning (with no intent of
> future removal), or do we want to still go through the RFC here?

I think it's ok to just add the warning, unless somebody objects.
-- 
Stas Malyshev
smalyshev@gmail.com

---

## #102592 — Yasuo Ohgaki — 2018-07-04T22:14:04+00:00

URL: https://externals.io/message/102393#102592 · In reply to: #102393

> Hi internals,
>
> Another small deprecation for your consideration...
>
> https://wiki.php.net/rfc/continue_on_switch_deprecation
>
> Regards,
> Nikita
>

This great improvement. IMO.

Since the issue is incompatibility between current "continue" and "break",
how about provide a tool replace "continue" to "break" where it is
applicable.
(Raise error for invalid "continue" usage also. This would be a bug most
likely.)

Then there wouldn't be compatibility issue anymore.

Regards,

--
Yasuo Ohgaki
yohgaki@ohgaki.net

---

## #102593 — Kalle Sommer Nielsen — 2018-07-04T22:20:36+00:00

URL: https://externals.io/message/102393#102593 · In reply to: #102592

Den tor. 5. jul. 2018 kl. 00.15 skrev Yasuo Ohgaki <yohgaki@ohgaki.net>:
> Since the issue is incompatibility between current "continue" and "break",
> how about provide a tool replace "continue" to "break" where it is
> applicable.
> (Raise error for invalid "continue" usage also. This would be a bug most
> likely.)
>
> Then there wouldn't be compatibility issue anymore.

I like the idea of a tool for such, but I think its better to
implement it in some of the static analyzer projects like PHPStan and
the like to catch these.



-- 
regards,

Kalle Sommer Nielsen
kalle@php.net

---

Next page: https://externals.io/message/102393.md?page=2
