pass multiple inputs to proc_open pipe in php

I am trying to create an interactive terminal to use in a project that I am working on. the idea is that I will have a web page which looks like a terminal and can use it to work with command line tools. anyway I need to use php as my service was built on php and I am familiar with it , but I couldn't get to a point where I can send multiple commands to bash (I am using bash for testing) , here is my last code snippet which seems to be able to send the given commands but I am not sure because the output of the commands is 1 !

<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);


$descs = array(

    0 => array("pipe", "r"),
    1 => array("pipe", "w"),
    2 => array("file", "error.txt", "w")
);


$cmd = "/bin/bash";


$proc = proc_open($cmd, $descs, $pipe, NULL);
stream_set_blocking($pipe[0], 0);
stream_set_blocking($pipe[1], 0);

if(!$proc) {
$error_file = fopen("error.txt", "r");
echo "Error !" . fread($error_file, filesize($error_file));
fclose($error_file);
exit();
}

$status = proc_get_status($proc);
print_r($status);

if(is_resource($proc)) {

$commands = ["id", "whoami", "ls"]; 
$n = 0;
$output = "";
while($n < 3) {
    echo $commands[$n];
    usleep(1000);   
    fwrite($pipe[0], $commands[$n] . PHP_EOL );
    fflush($pipe[0]);
    usleep(1000);

    while($out = stream_get_contents($pipe[1]) && !feof($pipe[1])){

        $output .= $out ;

    }
    echo $output;   
    $n++;
}

} 
?>