Discover how a tiny syntax change can make your PHP code cleaner and faster to write!
Why Arrow functions (short closures) in PHP? - Purpose & Use Cases
Imagine you want to quickly create a small function inside another function to process some data, like filtering or mapping an array. You write a full function with all the usual syntax, even though it's just a tiny task.
Writing full functions for small tasks takes extra lines, makes your code bulky, and harder to read. It's easy to make mistakes copying variables or forgetting to return values. This slows you down and makes your code messy.
Arrow functions let you write tiny functions in one line, capturing variables automatically. This makes your code shorter, cleaner, and easier to understand at a glance.
$result = array_map(function($x) use ($factor) { return $x * $factor; }, $numbers);$result = array_map(fn($x) => $x * $factor, $numbers);
You can write quick, clear, and concise functions inside other functions without extra clutter or mistakes.
When processing a list of prices to apply a discount, arrow functions let you write the calculation in one short line, making your code neat and easy to update.
Manual small functions are long and error-prone.
Arrow functions shorten code and auto-capture variables.
They make your code cleaner and faster to write.
Practice
Solution
Step 1: Understand arrow function syntax
Arrow functions in PHP use the 'fn' keyword and automatically return the expression value without needing 'return'.Step 2: Compare features
Unlike regular closures, arrow functions can access variables from the parent scope without 'use'.Final Answer:
They automatically return the value of the expression without needing the return keyword. -> Option AQuick Check:
Arrow functions auto-return expression [OK]
- Thinking arrow functions need 'return' keyword
- Believing arrow functions can't access outside variables
- Confusing arrow functions with regular anonymous functions
Solution
Step 1: Identify correct arrow function syntax
Arrow functions use 'fn' keyword, parentheses for parameters, and '=>' followed by expression without braces.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'.Final Answer:
$double = fn($x) => $x * 2; -> Option BQuick Check:
Arrow function syntax = fn(params) => expression [OK]
- Using braces {} with arrow functions
- Adding 'return' keyword inside arrow functions
- Omitting parentheses around parameters
$factor = 3;
$multiply = fn($x) => $x * $factor;
echo $multiply(5);
Solution
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.Step 2: Calculate the output
Calling $multiply(5) computes 5 * 3 = 15, which is printed.Final Answer:
15 -> Option DQuick Check:
Arrow functions capture outside variables automatically [OK]
- Expecting 'use' keyword to access outside variables
- Thinking $factor is undefined inside arrow function
- Confusing arrow functions with regular closures
$a = 10;
$fn = fn($x) => { return $x + $a; };
echo $fn(5);
Solution
Step 1: Check arrow function syntax rules
Arrow functions use 'fn' with '=>' followed by a single expression without braces or 'return'.Step 2: Identify the error in the code
The code uses braces {} and 'return' inside an arrow function, which is invalid syntax.Final Answer:
Arrow functions cannot have braces {} around the expression. -> Option CQuick Check:
Arrow functions = no braces, no return [OK]
- Adding braces {} around arrow function body
- Using 'return' inside arrow functions
- Thinking 'use' is needed for outside variables in arrow functions
$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);
Solution
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).Step 2: Determine map operation
We want to double the filtered numbers, so multiply by 2: $n * 2.Step 3: Check options
$n > 4; $n * 2 uses $n > 4 for filter and $n * 2 for map, matching requirements exactly.Final Answer:
$n > 4; $n * 2 -> Option AQuick Check:
Filter >=5 = $n > 4, double = $n * 2 [OK]
- Using wrong comparison operator in filter
- Adding instead of multiplying in map
- Including numbers less than 5 by mistake
Ready to prove it?
Pass the test and this pattern moves unknown
