Junior — Middle
How can an external parameter be passed to an anonymous function?
sobes.tech AI
Answer from AI
In PHP, the keyword use is used to pass external parameters into an anonymous function. It allows capturing variables from the outer scope.
Example:
$message = "Hello";
$func = function($name) use ($message) {
echo "$message, $name!";
};
$func("World"); // Outputs: Hello, World!
Here, the variable $message from the outer scope is passed into the anonymous function via use. Without this keyword, the anonymous function does not have access to external variables.