When expressions
A when expression produces a value, and each branch is a full statement block. That is the difference from match: you can validate, loop, and build intermediate values before returning the result:
report.ppphp
int $total = when ($orders !== []) {
int $sum = 0;
foreach ($orders as int $order) {
$sum = $sum + $order;
}
return $sum;
} else {
return 0;
};
A final else is required, every reachable branch must yield a value or terminate, and branch-local variables like $sum do not escape. Press Run to execute it, and Build PHP to see the generated PHP control flow.