Bird
Raised Fist0
PHPprogramming~20 mins

Arrow functions (short closures) in PHP - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Arrow Function Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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);
?>
AArray ( [0] => 3 [1] => 6 [2] => 9 )
BArray ( [0] => 1 [1] => 2 [2] => 3 )
CArray ( [0] => 0 [1] => 0 [2] => 0 )
DFatal error: Uncaught Error: Undefined variable: multiplier
Attempts:
2 left
💡 Hint
Remember that arrow functions automatically capture variables from the parent scope.
❓ Predict Output
intermediate
1:30remaining
Arrow function with multiple parameters
What will this PHP code output?
PHP
<?php
$adder = fn($a, $b) => $a + $b;
echo $adder(5, 7);
?>
AError: Too few arguments
B57
C12
DError: Syntax error
Attempts:
2 left
💡 Hint
Arrow functions can take multiple parameters just like normal functions.
🔧 Debug
advanced
2: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);
?>
AOutputs 12 because arrow functions use the variable's value at call time
BOutputs 8 because arrow functions capture variables by value at definition time
CFatal error: Cannot use string 'three' as a number
DParse error: syntax error
Attempts:
2 left
💡 Hint
Arrow functions capture variables from the parent scope by value, not by reference.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in arrow function usage
Which option contains a syntax error in the arrow function?
A$f = fn($x) => $x + 1;
B$f = fn($x) => $x * 2;
C$f = fn($x) { return $x * 2; };
D$f = fn($x) => { $x * 2; };
Attempts:
2 left
💡 Hint
Arrow functions must have a single expression without braces or use return inside braces in normal closures.
🚀 Application
expert
3: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
?>
Aarray_map(fn($n) => $n * $n, array_filter($nums, fn($n) => $n % 2 === 0));
B;)0 ==! 2 % n$ >= )n$(nf ,)smun$ ,n$ * n$ >= )n$(nf(pam_yarra(retlif_yarra
Carray_map(fn($n) => $n * $n, array_filter($nums, fn($n) => $n % 2));
Darray_filter(array_map(fn($n) => $n * $n, $nums), fn($n) => $n % 2 !== 0);
Attempts:
2 left
💡 Hint
Filter first to keep even numbers, then map to square them.

Practice

(1/5)
1. What is a key feature of PHP arrow functions (short closures)?
easy
A. They automatically return the value of the expression without needing the return keyword.
B. They require the use of the 'function' keyword every time.
C. They cannot access variables from the parent scope.
D. They must always have multiple statements inside the function body.

Solution

  1. Step 1: Understand arrow function syntax

    Arrow functions in PHP use the 'fn' keyword and automatically return the expression value without needing 'return'.
  2. Step 2: Compare features

    Unlike regular closures, arrow functions can access variables from the parent scope without 'use'.
  3. Final Answer:

    They automatically return the value of the expression without needing the return keyword. -> Option A
  4. Quick Check:

    Arrow functions auto-return expression [OK]
Hint: Arrow functions auto-return expressions without 'return' keyword [OK]
Common Mistakes:
  • Thinking arrow functions need 'return' keyword
  • Believing arrow functions can't access outside variables
  • Confusing arrow functions with regular anonymous functions
2. Which of the following is the correct syntax for a PHP arrow function that doubles a number?
easy
A. $double = function($x) { $x * 2; };
B. $double = fn($x) => $x * 2;
C. $double = fn $x => return $x * 2;
D. $double = fn($x) { return $x * 2; };

Solution

  1. Step 1: Identify correct arrow function syntax

    Arrow functions use 'fn' keyword, parentheses for parameters, and '=>' followed by expression without braces.
  2. Step 2: Check each option

    $double = fn($x) => $x * 2; matches correct syntax: fn($x) => $x * 2;. The other options use regular function syntax, braces incorrectly, or lack parentheses and misuse 'return'.
  3. Final Answer:

    $double = fn($x) => $x * 2; -> Option B
  4. Quick Check:

    Arrow function syntax = fn(params) => expression [OK]
Hint: Arrow functions use 'fn(params) => expression;' syntax [OK]
Common Mistakes:
  • Using braces {} with arrow functions
  • Adding 'return' keyword inside arrow functions
  • Omitting parentheses around parameters
3. What will be the output of the following PHP code?
$factor = 3;
$multiply = fn($x) => $x * $factor;
echo $multiply(5);
medium
A. 0
B. Error: Undefined variable
C. 5
D. 15

Solution

  1. Step 1: Understand variable scope in arrow functions

    Arrow functions automatically capture variables from the parent scope, so $factor = 3 is accessible inside the function.
  2. Step 2: Calculate the output

    Calling $multiply(5) computes 5 * 3 = 15, which is printed.
  3. Final Answer:

    15 -> Option D
  4. Quick Check:

    Arrow functions capture outside variables automatically [OK]
Hint: Arrow functions capture outside variables automatically [OK]
Common Mistakes:
  • Expecting 'use' keyword to access outside variables
  • Thinking $factor is undefined inside arrow function
  • Confusing arrow functions with regular closures
4. Identify the error in this PHP code using an arrow function:
$a = 10;
$fn = fn($x) => { return $x + $a; };
echo $fn(5);
medium
A. Arrow functions cannot use variables from outside scope.
B. Missing 'use' keyword to access $a inside the function.
C. Arrow functions cannot have braces {} around the expression.
D. Arrow functions must have a semicolon after the expression.

Solution

  1. Step 1: Check arrow function syntax rules

    Arrow functions use 'fn' with '=>' followed by a single expression without braces or 'return'.
  2. Step 2: Identify the error in the code

    The code uses braces {} and 'return' inside an arrow function, which is invalid syntax.
  3. Final Answer:

    Arrow functions cannot have braces {} around the expression. -> Option C
  4. Quick Check:

    Arrow functions = no braces, no return [OK]
Hint: Arrow functions do not use braces or 'return' keyword [OK]
Common Mistakes:
  • Adding braces {} around arrow function body
  • Using 'return' inside arrow functions
  • Thinking 'use' is needed for outside variables in arrow functions
5. Given this array of numbers, which arrow function code correctly filters out all numbers less than 5 and then doubles the remaining numbers?
$numbers = [2, 4, 5, 7, 1];
// Fill in the blanks:
$filtered = array_filter($numbers, fn($n) => ________);
$doubled = array_map(fn($n) => ________, $filtered);
print_r($doubled);
hard
A. $n > 4; $n * 2
B. $n > 5; $n + 2
C. $n > 5; $n * 2
D. $n >= 4; $n * 2

Solution

  1. Step 1: Determine filter condition

    We want to keep numbers 5 or greater. Since 5 is included, condition is $n > 4 (which means 5 and above).
  2. Step 2: Determine map operation

    We want to double the filtered numbers, so multiply by 2: $n * 2.
  3. Step 3: Check options

    $n > 4; $n * 2 uses $n > 4 for filter and $n * 2 for map, matching requirements exactly.
  4. Final Answer:

    $n > 4; $n * 2 -> Option A
  5. Quick Check:

    Filter >=5 = $n > 4, double = $n * 2 [OK]
Hint: Filter with $n > 4 to include 5, then multiply by 2 [OK]
Common Mistakes:
  • Using wrong comparison operator in filter
  • Adding instead of multiplying in map
  • Including numbers less than 5 by mistake
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard