...
forEach() is like every other function in that it returns a value.
- Its return value is a list containing the last statement executed in the last each iteration.
- All the $-variables used in the call to forEach() keep their values between iterations.
- All the $-variables used in the call to forEach() will be available after it has returned.
...
Code Block |
---|
forEach($number, $numberList,
$currentValue = $number*2
), |
Returns [2,4,6,8,10,12,14,16,18,20], as forEach() returns the value of the final expression to be evaluated in its final each iteration.
Code Block |
---|
do( forEach($number, $numberList, $sumOfNumbers = $sumOfNumbers + $number ), $sumOfNumbers ) |
Returns 55. The forEach will return the value at each iteration ([1, 3, 6, 10, 15, 21, 28, 36, 45, 55, as the variable $sumOfNumbers persists in-between forEach iterations]), the do then returns the final value of $sumOfNumbers.
Detailed example: (step-by-step)
...