PHP Functional Programming Scope

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!


In PHP, an anonymous function has its own scope like any other PHP function.

In JavaScript, an anonymous function can access a variable in outside scope. But in PHP, this is not permitted.

$name = 'John';

// Anonymous function trying access outside scope
$sayHello = function() {
    return "Hello $name!";

print $sayHello('John'); // Hello !
// With notices active, there is also an Undefined variable $name notice

Got any PHP Question?