0
0
PHPprogramming~5 mins

Arrow functions (short closures) in PHP - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is an arrow function in PHP?
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:<br>
$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
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
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
What does this arrow function return?<br>
$add = fn($a, $b) => $a + $b;<br>echo $add(3, 4);
A7
B34
CError
Dnull
Which of the following is true about arrow functions compared to regular anonymous functions?
AArrow functions require 'use' to access variables
BArrow functions have implicit return
CArrow functions can have multiple statements
DArrow functions cannot access parent scope variables
Explain how arrow functions differ from regular anonymous functions in PHP.
Think about syntax and how variables are accessed.
You got /4 concepts.
    Write a simple arrow function in PHP that multiplies a number by 5.
    Use fn(parameter) => expression;
    You got /3 concepts.