PHP: how to get variable from function in another

2019-09-10 03:59发布

This question already has an answer here:

Example

function a(){
    $num = 1;

    function b(){
        echo $num; // how to get $num value?
    }
}

In this case global not working, because $num isn't global variable.

2条回答
该账号已被封号
2楼-- · 2019-09-10 04:35

You could use the S_SESSION to get the variable?

function a(){
    $_SESSION['num'] = 1;

    function b(){
        echo $_SESSION['num'];
    }
}

Not sure nested function is the way to go btw.

查看更多
别忘想泡老子
3楼-- · 2019-09-10 04:40
function a() {
    $num = 1;
    function b($num) {
        echo $num;
    };
    b($num);
}
a();
查看更多
登录 后发表回答