A PHP for
-loop is defined like so:
for (expr1; expr2; expr3)
statement
Here expr1
and expr3
can also be a comma-separated list of expressions, which makes sense, e.g.,
for ($i = 0, $j = 1; $i < 3; $i++, $j++)
echo $i, $j, PHP_EOL;
will output:
01
12
23
However, expr2
is also allowed to be a list of expressions, and it is not completely clear why this should be possible. For instance,
for ($i = 0, $j = 1; $i < 3, $j < 2; $i++, $j++)
echo $i, $j, PHP_EOL;
outputs only
01
Testing around a little bit, it seems that when expr2
is a list of expressions, they are all evaluated, but only the return value of the last expression is used to decide whether to continue iterating.
Is there any realistic scenario where having a list of expressions as the loop's guard actually makes sense, i.e., is there any good reason that this is possible? Or is this more like an artefact of the language?