📖 Scenario: You are creating a simple PHP script to understand how closures can remember variables from outside their scope using the use keyword. This is like packing a small bag with some items before going on a trip, so you have them ready inside your function.
🎯 Goal: Build a PHP closure that remembers a variable from outside its function using use and then prints a message including that variable.
📋 What You'll Learn
Create a variable with a string value.
Create a closure that uses the variable with use.
Call the closure to print a message including the variable.
Use exact variable and function names as instructed.
💡 Why This Matters
🌍 Real World
Closures with variable binding are useful when you want to create small functions that remember some data from outside, like setting up a personalized greeting or a callback with extra information.
💼 Career
Understanding closures and variable binding is important for PHP developers to write clean, reusable, and flexible code, especially when working with callbacks, event handlers, or functional programming styles.
Progress0 / 4 steps
1
Create a variable with a greeting message
Create a variable called $greeting and set it to the string "Hello".
PHP
Hint
Use $greeting = "Hello"; to create the variable.
2
Create a closure that uses the greeting variable
Create a closure called $sayHello that uses the variable $greeting with use. The closure should take one parameter $name and return the string combining $greeting, a space, and $name.
PHP
Hint
Use function($name) use ($greeting) to create the closure.
3
Call the closure with a name
Call the closure $sayHello with the argument "Alice" and store the result in a variable called $message.
PHP
Hint
Call the closure like a function: $sayHello("Alice").
4
Print the message
Print the variable $message using echo.
PHP
Hint
Use echo $message; to print the message.
Practice
(1/5)
1. What does the use keyword do in a PHP closure?
easy
A. It defines a new variable inside the closure.
B. It creates a global variable accessible everywhere.
C. It imports variables from the parent scope into the closure.
D. It calls the closure immediately.
Solution
Step 1: Understand closure scope
A closure is a function inside another function that can access variables from the outside only if explicitly told.
Step 2: Role of use
The use keyword brings variables from the parent scope into the closure's scope so the closure can use them.
Final Answer:
It imports variables from the parent scope into the closure. -> Option C
Quick Check:
use = import variables [OK]
Hint: Remember: use imports outside variables into closure [OK]
Common Mistakes:
Thinking use creates new variables inside closure
Confusing use with global keyword
Assuming use calls the closure
2. Which of the following is the correct syntax to pass a variable $x by reference into a closure using use?
easy
A. function() use ($x) { }
B. function() use (&x) { }
C. function() use ($&x) { }
D. function() use (&$x) { }
Solution
Step 1: Understand passing by reference
To allow a closure to modify an outside variable, it must be passed by reference using &.
Step 2: Correct syntax with use
The correct syntax is use (&$x) where & is before the variable name with the dollar sign.
Final Answer:
function() use (&$x) { } -> Option D
Quick Check:
Reference = & before $variable [OK]
Hint: Use & before $var inside use to pass by reference [OK]
Common Mistakes:
Placing & after $ or before variable name without $