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?
✗ Incorrect
Arrow functions in PHP are declared using the 'fn' keyword.
How do arrow functions capture variables from the parent scope?
✗ Incorrect
Arrow functions automatically capture variables from the parent scope by value.
Can arrow functions contain multiple statements in PHP?
✗ Incorrect
Arrow functions can only contain a single expression; multiple statements require regular anonymous functions.
What does this arrow function return?<br>
$add = fn($a, $b) => $a + $b;<br>echo $add(3, 4);
✗ Incorrect
The arrow function adds 3 and 4, returning 7.
Which of the following is true about arrow functions compared to regular anonymous functions?
✗ Incorrect
Arrow functions have implicit return and automatically capture variables by value.
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.