Run process with realtime output in PHP

In PHP, you can use the proc_open function to run a process and read its output in real-time. Here is an example of how you can use it:

<?php

$descriptorspec = [
  0 => ["pipe", "r"], // stdin is a pipe that the child will read from
  1 => ["pipe", "w"], // stdout is a pipe that the child will write to
  2 => ["pipe", "w"], // stderr is a pipe that the child will write to
];

$process = proc_open('echo "Hello World!"', $descriptorspec, $pipes);

if (is_resource($process)) {
  while ($s = fgets($pipes[1])) {
    print $s;
  }
  proc_close($process);
}

Watch a course Learn object oriented PHP

This example runs the command (echo "Hello World!") and reads its output and error streams in real-time using the fgets function. The output is then printed to the screen. Note that in this example, $descriptorspec is an array containing descriptor arrays. Each descriptor array contains a pipe type, either "pipe" for read or "w" for write, and corresponding pipe.