Scope

suggest change

In 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

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents