Yes, you can create anonymous functions in PHP that execute immediately without polluting the global namespace;
call_user_func(function() { $a = 'hi'; echo $a; });
The syntax is not as pretty as the Javascript equivalent, but it does the same job. I think the design is very useful and often uses it.
You can also return the following values:
$str = call_user_func(function() { $a = 'foo'; return $a; }); echo($str);
Nigel alderton
source share