duanhongxian6982 2018-08-29 16:32
浏览 82

即使变量是全局变量,变量也不能在PHP中的函数下工作

I try to define variable and then call it in my function, and then i call the function in another file. So, the variables outside the function work properly, but when i call them in function they don't work. Here's the code.

$info = new ServerInfo($server->GetServerData('IPAdress'));
$id = $server->GetServerData('ID');
$sshhost = $server->GetServerData('SSHHOST');
$sshport = $server->GetServerData('SSHPORT');
$sshuser = $server->GetServerData('SSHUSER');
$sshpw = base64_decode(base64_decode($server->GetServerData('SSHPW')));
$port = $server->GetServerData('PORT');

/* Start Server Function */
function start_server($sshhost, $sshport, $sshuser, $sshpw){

global $sshhost;
global $sshport;
global $sshuser;
global $sshpw;
global $id;
global $port;

if (!function_exists("ssh2_connect")) return "SSH2 PHP extenzija nije instalirana";

if(!($con = ssh2_connect($sshhost, $sshport))){
    return "Ne mogu se spojiti na server";
} else {

    if(!ssh2_auth_password($con, $sshuser, $sshpw)) {
        return "Netačni podatci za prijavu";
    } else {

        $stream = ssh2_shell($con, 'vt102', null, 80, 24, SSH2_TERM_UNIT_CHARS);
        fwrite( $stream, "cd /home/cs && screen -A -m -S srv".$id. " ./hlds_run -console -game cstrike +port " .$port. " +map de_dust2 +maxplayers 32 -pingboost 1".PHP_EOL);
        sleep(1);
        echo "Server Startovan";

        return TRUE;

    }
}   
}
  • 写回答

1条回答

  • doubaomao9304 2018-08-29 16:42
    关注

    You can declare them using $GLOBALS['variablename'] then still use the $variablename in other function. ref: https://www.w3schools.com/php/php_superglobals.asp How to declare a global variable in php?

    function start_server($sshhost, $sshport, $sshuser, $sshpw){
    
    $GLOBALS['sshhost'] = $sshhost;
    $GLOBALS['sshport'] = $sshport;
    $GLOBALS['sshuser'] = $sshuser;
    $GLOBALS['sshpw'] = $sshpw;
    
    /* do more stuff */
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog