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?
That’s actually the case. When you call a function, the parameter you pass is actually a copy, and the value is copied, which is equivalent to another variable and has no relationship.
The same goes for anonymous functions. But if you want to use external variables in anonymous functions, they cannot be accessed.