0
0
PHPprogramming~5 mins

Closures and variable binding with use in PHP - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a closure in PHP?
A closure is an anonymous function that can capture variables from its surrounding scope, allowing it to use those variables even when called outside that scope.
Click to reveal answer
beginner
How does the use keyword work in PHP closures?
The use keyword lets a closure access variables from the parent scope by copying their values into the closure's scope.
Click to reveal answer
intermediate
What happens if you modify a variable inside a closure without use?
Without use, the closure cannot access or modify variables from the parent scope. Variables inside the closure are local and separate.
Click to reveal answer
intermediate
How can you modify a variable from the parent scope inside a closure?
You can pass the variable by reference using use (&$var). This allows the closure to change the original variable's value.
Click to reveal answer
intermediate
Explain the difference between passing variables by value and by reference in closures.
Passing by value copies the variable's value into the closure, so changes inside don't affect the original. Passing by reference shares the variable, so changes inside the closure update the original variable.
Click to reveal answer
What does the use keyword do in a PHP closure?
ADeclares a global variable inside the closure
BDefines the return type of the closure
CAllows the closure to access variables from the parent scope
DCreates a new variable inside the closure
How do you allow a closure to modify a variable from the parent scope?
ADeclare the variable as global inside the closure
BPass the variable by reference using <code>use (&$var)</code>
CUse <code>global $var</code> outside the closure
DYou cannot modify parent variables inside closures
What happens if you omit use when trying to access a parent variable inside a closure?
AThe variable is treated as a global variable
BThe closure automatically accesses the variable
CThe variable is copied by default
DThe closure cannot access the variable and will cause an error
Which of the following is a correct way to create a closure that uses a variable $x from the parent scope?
Afunction() use ($x) { return $x * 2; }
Bfunction($x) { return $x * 2; }
Cfunction() { return $x * 2; }
Dfunction() global $x { return $x * 2; }
If you want to keep the original variable unchanged, how should you pass it to a closure?
APass it by value using <code>use ($var)</code>
BPass it by reference using <code>use (&$var)</code>
CDeclare it as global inside the closure
DYou cannot keep it unchanged
Explain how the use keyword works with closures in PHP and why it is needed.
Think about how closures get variables from outside their own function.
You got /5 concepts.
    Describe the difference between passing variables by value and by reference in PHP closures and give an example of when to use each.
    Consider if you want the closure to change the original variable or not.
    You got /5 concepts.