Back to questions
JuniorTheoryCommon
What does the program print?
<?php
$bonus = 5;
$pay = fn(int $base) => $base + $bonus;
$bonus = 50;
$len = strlen(...);
echo $pay(100), ' ', $len('abcd'), "\n";105 4
- Expecting
150, as iffnread$bonuswhen called: an arrow function captures by value at creation, so$bonus = 50later does not reach it. - Expecting an arrow function to need
use, like an anonymous one:fncaptures every outer variable its expression mentions, automatically.
- How would you make the function see the new value of
$bonus? - How does
strlen(...)differ from the string'strlen'used as a callable?