function a()
{
$demi = '局部变量';
b($demi);
}
function b($args)
{
echo $args;
}
a();
All functions and classes in PHP have global scope and can be defined within a function and called outside, and vice versa.
Why can function b obtain the local variables of function a by passing parameters?
function tesxt()
{
$var = 10;
$echonumber = function($num) {
echo $num;
};
$echonumber($var);
}
tesxt();
Similarly, why do anonymous functions also obtain variables of external functions by passing parameters?
其实是这样的。当你调用函数的时候,你传的参数其实是一个副本,值复制,相当于另外一个变量,没有任何关系。
同理匿名函数也是一样的。但是你如果想要在匿名函数中使用外部变量,是无法访问的。