即使变量是全局变量,

时间:2018-08-29 16:32:13

标签: php global

我尝试定义变量,然后在我的函数中调用它,然后在另一个文件中调用该函数。因此,函数外部的变量可以正常工作,但是当我在函数中调用它们时,它们将不起作用。这是代码。

$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 个答案:

答案 0 :(得分:1)

您可以使用 let searchResult = response.hits.hits; console.log(searchResult); for(let i = 0; i < searchResult.length; i++) { //This line displays the name.id properly document.getElementById("name").value = searchResult[i]._source.type.name.id; //this line gives undefined document.getElementById("color").value = searchResult[i]._source.color[i].color_type; }) } 声明它们,然后在其他函数中仍然使用$GLOBALS['variablename']。 参考: https://www.w3schools.com/php/php_superglobals.asp How to declare a global variable in php?

$variablename