Is it possible to access outer local varialbe in a PHP sub-function?
In below code, I want to access variable $l in inner function bar. Declaring $l as global $l in bar doesn’t work.
function foo()
{
$l = "xyz";
function bar()
{
echo $l;
}
bar();
}
foo();
You could probably use a Closure, to do just that…
Edit : took some time to remember the syntax, but here’s what it would look like :
And, running the script, you’d get :
A couple of note :
;after the function’s declaration !usethe variable by reference, with a&before it’s name :use (& $l)For more informations, as a reference, you can take a look at this page in the manual : Anonymous functions