I have a script that runs one command. Based on that result, I can run the next command. Steps:run first script it puts me in global then from global I will run the next command
you can do like this
$stream = ssh2_exec($connection, 'command1;command2');