Scope
suggest changeIn PHP, an anonymous function has its own scope like any other PHP function.
In JavaScript, an anonymous function can access a variable in an outer scope. In PHP, an outer variable is available only when the closure lists it with use.
$name = 'John';
// Anonymous function trying access outside scope
$sayHello = function() {
return "Hello $name!";
};
print $sayHello('John'); // Hello !
// With warnings active, this also raises Warning: Undefined variable $name
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents