Re: [DISCUSSION] Proposal: Introduce a loop statement
| From: | Ilia | Date: | Tue, 01 Dec 2026 22:19:26 +0000 |
| Subject: | Re: [DISCUSSION] Proposal: Introduce a loop statement | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-133279@lists.php.net to get a copy of this message | ||
On Fri, Nov 27, 2026 at 3:09 PM Piotr Ziarek <elvenar@10g.pl> wrote:
> Hello PHP Internals,
>
> I would like to propose introducing a new language construct called loop,
> designed for executing a block of code a specified number of times.
>
> The proposed syntax is:
>
>
> loop(10) {
> // Code executed exactly 10 times
> }
>
> The motivation is to provide a simple and readable alternative to using a
> for loop when the iteration counter is not needed.
>
> Currently, developers typically write:
>
>
> for ($i = 0; $i < 10; $i++) {
> doSomething();
> }
>
> With the proposed construct:
>
>
> loop(10) {
> doSomething();
> }
>
> The initial proposal is intentionally minimal:
>
> -
>
> No iteration counter.
> -
>
> The argument specifies the exact number of iterations.
> -
>
> The argument is evaluated only once.
> -
>
> break and continue should behave as they do in existing loops.
> -
>
> A count of zero results in no iterations.
>
> This construct is not intended to replace for, foreach or while, but to
> provide a simpler alternative for a common use case.
>
> I would appreciate feedback on whether this proposal would be worth
> pursuing as a formal PHP RFC.
>
> At this stage, I am proposing the concept and would also welcome advice
> regarding implementation in PHP Core.
>
> Best regards
>
It seems odd to me; the for() loop might be more "text", but it is pretty
much a universal language construct across many languages and is super
familiar. Minimizing it to loop(x) saves a few chars but introduces a WTF
factor that makes it not worth it.
--
Ilia Alshanetsky
Technologist, CTO, Entrepreneur
E: ilia@ilia.ws
T: @iliaa
B: http://ilia.ws