An arrow function is a short way to write anonymous functions using the fn keyword. It automatically captures variables from the parent scope without needing the 'use' keyword.
Click to reveal answer
beginner
How do arrow functions capture variables from the parent scope?
Arrow functions automatically capture variables from the parent scope by value, so you don't need to specify them with 'use'.
Click to reveal answer
beginner
Rewrite this anonymous function as an arrow function:
$double = function($x) { return $x * 2; };
$double = fn($x) => $x * 2;
Click to reveal answer
intermediate
Can arrow functions contain multiple statements in PHP?
No, arrow functions can only contain a single expression. For multiple statements, use a regular anonymous function.
Click to reveal answer
intermediate
What is the difference between arrow functions and regular anonymous functions in PHP?
Arrow functions use the fn keyword, have implicit returns, and automatically capture variables by value. Regular anonymous functions use the function keyword and require 'use' to access variables from the parent scope.
Click to reveal answer
Which keyword is used to declare an arrow function in PHP?
Afn
Bfunction
Carrow
Dlambda
✗ Incorrect
Arrow functions in PHP are declared using the 'fn' keyword.
How do arrow functions capture variables from the parent scope?
AYou must specify variables with 'use'
BAutomatically by reference
CAutomatically by value
DThey cannot access parent scope variables
✗ Incorrect
Arrow functions automatically capture variables from the parent scope by value.
Can arrow functions contain multiple statements in PHP?
AYes, with curly braces
BYes, if you use 'return'
COnly if declared inside a class
DNo, only a single expression
✗ Incorrect
Arrow functions can only contain a single expression; multiple statements require regular anonymous functions.
What does this arrow function return?
$add = fn($a, $b) => $a + $b; echo $add(3, 4);
A7
B34
CError
Dnull
✗ Incorrect
The arrow function adds 3 and 4, returning 7.
Which of the following is true about arrow functions compared to regular anonymous functions?
AArrow functions require 'use' to access 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
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 B
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?