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
Quoted text · 25 lines
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
Hi Piotr,
So, it would be equivalent to something like this:
function loop(int $, callable $iter) {
while($--) $iter();
}
loop(10, fn () => print 'hello'. "\n");
It would be a neat syntax sugar. Generally, when we add complexity to the language (new syntax), we do it only when the advantages justify it. Adding loop(), even if it is just a compile-time translation, would mean every future change needs to account for it. To win proponents of such a proposal, you need to answer questions such as:
- How often is this a problem developers struggle with?
- How many parts of the language are affected by the introduction of the new syntax? What changes will existing PHP projects need to undertake when the new syntax lands?
- Can we get more out of it when it is part of the language, e.g. speed, better static syntax analysis, more security?
- How contentious is the change? Will people argue about the details of the proposal? E.g. Why no implicit counter? Why this name and not another?
- What implementation challenges may there be? E.g. how does it work with goto or exceptions?
In other words: you need to sell us the idea. Running an idea by a large group is ok, but people are less likely to buy into something without a more ironed-out concept. These are the details you will need to include in your potential RFC.
Regards,
Kamil Tekiela