Challenge - 5 Problems
Arrow Function Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple arrow function
What is the output of the following PHP code using an arrow function?
PHP
<?php $multiplier = 3; $numbers = [1, 2, 3]; $results = array_map(fn($n) => $n * $multiplier, $numbers); print_r($results); ?>
Attempts:
2 left
💡 Hint
Remember that arrow functions automatically capture variables from the parent scope.
✗ Incorrect
The arrow function multiplies each number by the multiplier variable from the outer scope, producing 3, 6, and 9.
❓ Predict Output
intermediate1:30remaining
Arrow function with multiple parameters
What will this PHP code output?
PHP
<?php $adder = fn($a, $b) => $a + $b; echo $adder(5, 7); ?>
Attempts:
2 left
💡 Hint
Arrow functions can take multiple parameters just like normal functions.
✗ Incorrect
The arrow function adds 5 and 7, outputting 12.
🔧 Debug
advanced2:30remaining
Why does this arrow function cause an error?
Consider this PHP code snippet. What error will it produce and why?
PHP
<?php $factor = 2; $func = fn($x) => $x * $factor; $factor = 'three'; echo $func(4); ?>
Attempts:
2 left
💡 Hint
Arrow functions capture variables from the parent scope by value, not by reference.
✗ Incorrect
Arrow functions capture variables by value when defined, so $factor is 2 inside the function even if changed later.
📝 Syntax
advanced2:00remaining
Identify the syntax error in arrow function usage
Which option contains a syntax error in the arrow function?
Attempts:
2 left
💡 Hint
Arrow functions must have a single expression without braces or use return inside braces in normal closures.
✗ Incorrect
Option D uses braces with an arrow function but does not return a value, causing a syntax error.
🚀 Application
expert3:00remaining
Using arrow functions to filter and transform arrays
Given the array $nums = [1, 2, 3, 4, 5, 6], which option produces an array of squares of even numbers only?
PHP
<?php $nums = [1, 2, 3, 4, 5, 6]; // Your code here ?>
Attempts:
2 left
💡 Hint
Filter first to keep even numbers, then map to square them.
✗ Incorrect
Option A filters even numbers first, then squares them, producing [4, 16, 36]. Others either filter odd numbers or filter after mapping incorrectly.