Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Recursive Arrow Function

<?php

$sum = fn($a, $sum) => empty($a) ? 0 : array_pop($a) + $sum($a, $sum);

print $sum([1,2,3], $sum);

?>

To make an arrow function recursive, it must include itself in the list of arguments. That way, it can call itself.

The intriguing point here is that the arrow function is defined based on the current variables, and the function holding variable is only defined after the arrow function.

See Also

PHP Features

Last updated: 09 November 2026