PHP nested function and anonymous function scope issues
phpcn_u1582
phpcn_u1582 2017-05-18 10:45:34
0
1
628
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?

phpcn_u1582
phpcn_u1582

reply all(1)
小葫芦

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template