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
Hi
is not intended to replace for, foreach or while, but to provide a simpler alternative for a common use case
The “common use case” is a claim that I'm not sure holds up in practice. My gut feeling is that of those three a for() loop is by far my least used loop in PHP with foreach() being the most widely used one. do-while() would likely below for().
Searching the company code base for for (, while (, foreach (, excluding tests:
- for: 50 results across 32 files.
- while: 70 results across 47 files.
- foreach: 1418 results across 492 files.
So the “by far” does not hold up, but the relative order does. A single digit number of the for() loops were not using the iteration variable within the loop itself, mostly those in debug code outside of test files.
I agree with Kamil that this requires much more evidence-based justification to become a discussable proposal.
Best regards
Tim Düsterhus
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.
I have some sympathy for this proposal, because I find the C-style for loop ugly and fiddly to use; but I agree with others that this is perhaps a bit too small a niche for dedicated syntax. Often, even when just iterating a fixed number of times, it's useful to have the iteration counter available, for things like debug logging or output styling (e.g. even/odd).
A more powerful approach would be to combine two new features:
-
A short-hand for creating a lazy range, e.g. [1..10]. This would produce an iterator object that produces sequential numbers, without allocating a full array, particularly useful for large and maybe even infinite ranges.
-
The option to omit the "as" clause from a foreach loop, so you can loop over an iterable without assigning its output anywhere.
That would allow all of the following:
// Process numbers 1 to 10 inclusive
foreach([1..10] as $i) {
doSomethingWithAValue($i);
}
// Just repeat 10 times
foreach([1..10]) {
doSomething();
}
// execute an iterator for a side effect and discard the value foreach($someIterator) {
// do nothing
}
Regards,
--
Rowan Tommins
[IMSoP]
Quoted text · 20 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.I have some sympathy for this proposal, because I find the C-style for loop ugly and fiddly to use; but I agree with others that this is perhaps a bit too small a niche for dedicated syntax. Often, even when just iterating a fixed number of times, it's useful to have the iteration counter available, for things like debug logging or output styling (e.g. even/odd).
A more powerful approach would be to combine two new features:
A short-hand for creating a lazy range, e.g. [1..10]. This would produce an iterator object that produces sequential numbers, without allocating a full array, particularly useful for large and maybe even infinite ranges.
The option to omit the "as" clause from a foreach loop, so you can loop over an iterable without assigning its output anywhere.
That would allow all of the following:
// Process numbers 1 to 10 inclusive
foreach([1..10] as $i) {
doSomethingWithAValue($i);
}// Just repeat 10 times
foreach([1..10]) {
doSomething();
}// execute an iterator for a side effect and discard the value foreach($someIterator) {
// do nothing
}
I concur. A shorthand range would be helpful in many places, and establish a syntax pattern that we want to use in pattern matching anyway. Allowing the as to be omitted seems like an easy change for someone interested, as there are already use cases like running out an iterator.
That would obviate the need for a dedicated construct for an edge case.
--Larry Garfield
Quoted text · 26 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
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