Why is my nested function not called from my anonymous function?

30 views Asked by At

I tried accomplishing a small task in PHP 7.4 (a language I never use) and decided to use a nested utility function isReverseScored, to be called inside an "arrow function", like so:

    // this is all inside a normal function, i.e. this is nested:
    function isReverseScored($questionIndex) { return in_array($questionIndex, $reverseScoredIndices); }

    $scores = array_map(
        fn ($value, $index) => isReverseScored($index) ? 8 - $value : $value,
        $values,
        array_keys($values));

array_map is called 18 times, but isReverseScored is never called. Why not?

The anonymous function always seems to evaluate to $value. Replacing the call with the actual body of isReverseScored gives the expected results:

        fn ($value, $index) => in_array($index, $reverseScoredIndices) ? 8 - $value : $value,

I declare another nested function (arithmeticMean) right next to the first one, which I can call just fine, albeit I'm not calling that one from within an anonymous function.

All of the above leads me to believe that I've declared the nested function correctly and called it in a valid position. I used a nested function because it appeared to be an available feature.

0

There are 0 answers