PHP: how to get variable from function in another function? [duplicate]

天大地大妈咪最大 提交于 2019-12-13 05:20:25

问题


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.


回答1:


function a() {
    $num = 1;
    function b($num) {
        echo $num;
    };
    b($num);
}
a();



回答2:


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.



来源:https://stackoverflow.com/questions/17860930/php-how-to-get-variable-from-function-in-another-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!