search

Home  >  Q&A  >  body text

How can a function within a function call a variable of the parent function?

var_dump(function_exists('b')); // false
function a(){
    $var = 123;
    function b(){
        //这里怎么调用 $var 变量?
        //不能将该函数改为匿名函数
    }
}

var_dump(function_exists('b')); // false
a();
var_dump(function_exists('b')); // true 这里必须为true

Requires that function b must be in function a, and function b must be a global variable

If b is changed to an anonymous function, $var can be called, but if an anonymous function is used, how can it be upgraded to What about global functions?

喵~喵~789 days ago672

reply all(0)I'll reply

No reply
  • Cancelreply