I have some code that I want to make into a function, as I want to use it with different parts of my code and on different pages, and I don’t want to have to have the code everywhere. I am using the PHPseclib library and class. The code that works on its own is:
set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib');
include('phpseclib/Net/SSH2.php');
$ssh = new Net_SSH2('$address');
if (!$ssh->login('$username', '$password')) {
exit('Login Failed');
}
$sPath = "minecraft/servers/";
$sSavingCode = "server.properties";
$motd = "test";
echo $ssh->exec("cat > $sPath$sSavingCode <<EOF
motd=".$motd."
EOF
");
I want to make this into a function, so I tried doing this:
set_include_path(get_include_path() . PATH_SEPARATOR . ‘phpseclib’);
include(‘phpseclib/Net/SSH2.php’);
$ssh = new Net_SSH2('$address');
if (!$ssh->login('$username', '$password')) {
exit('Login Failed');
}
function test($motd)
{
$sPath = "minecraft/servers/";
$sSavingCode = "server.properties";
$ssh->exec("cat > $sPath$sSavingCode <<EOF
motd=".$motd."
EOF
");
}
with the rest of the code outside and above the function. I am trying to call the function like:
$motd = "Server";
test($motd);
But that comes back with a server error. Is this function possible? Or should I just put the code where I need it every time I want to use it?
Your function has a dependency on
$ssh, so it should be passed as an argument: