Why are my variables going out of scope? PHP

后端 未结 2 1310
清酒与你
清酒与你 2021-01-22 20:49

Language is PHP 5.4 on an Apache 2.2 server. It\'s a script being called by ajax.

$usr = new User();

function getMyName(){
    echo $usr->username;
}
getMyNa         


        
2条回答
  •  一个人的身影
    2021-01-22 21:09

    $usr = new User();
    
    function getMyName(){
        global $usr;
        echo $usr->username;
    }
    getMyName();
    

    alternatively:

    $usr = new User();
    
    function getMyName($usr){
        echo $usr->username;
    }
    getMyName($usr);
    

提交回复
热议问题