I don’t understand why I don’t see the $ a variable, if I’ve indicated that it is global, does it have to come from the scope of the inFoo function?

How then to address her?

function inFoo() { $a = 5; function foo() { global $a; return $a; } return foo(); } echo '<pre>'; print_r(inFoo()); echo '</pre>'; 

    1 answer 1

    It is necessary to use closure and use :

     function inFoo() { $a = 5; $inner = function() use ($a) { return $a; }; return $inner(); } 

    http://php.net/manual/ru/functions.anonymous.php

    • Thanks for the method, I did, passed the variable a to the parameter - DivMan