title: How to capture capture child process output in PHP
body: <p>I need to capture the output of a child process in PHP to manipulate it.</p>
  
  <p>I've two little scripts, <code>parent.php</code> and <code>child.php</code></p>
  
  <p>This is the code of <code>parent.php</code></p>
  
  <pre><code>    $exe = $_SERVER['_'];
      $command = '/var/www/child.php';
  
      echo "\nSTART\n";
  
      $pid = pcntl_fork();
  
      ob_start();
  
      echo "\nHELLO\n";
  
      if ($pid == -1)
          exit("Could not fork");
      else if ($pid)
          pcntl_waitpid($pid, $status);
      else
      {
          $args = array
          (
              "arg1" =&gt; 'one',
              "arg2" =&gt; 'two',
              "arg3" =&gt; 'three'
          );
  
          pcntl_exec($exe, array($command), $args);
      }
  
      $content = ob_get_contents();
      ob_end_clean();
  
      echo "\nEND\n";
  
      echo $content . "\n";
  </code></pre>
  
  <p>And this is the code of <code>child.php</code></p>
  
  <pre><code>    echo "\n\tHELLO FROM TEST\n";
  
      echo "\t"; var_dump($_SERVER['arg1']);
      echo "\t"; var_dump($_SERVER['arg2']);
      echo "\t"; var_dump($_SERVER['arg3']);
  </code></pre>
  
  <p>Now... this is my output:</p>
  
  <pre><code>START
  
      HELLO FROM TEST
      string(3) "one"
      string(3) "two"
      string(5) "three"
  
  END
  
  HELLO
  </code></pre>
  
  <p>But I need <strong>THIS</strong> output</p>
  
  <pre><code>START
  
  END
  
  HELLO
  
      HELLO FROM TEST
      string(3) "one"
      string(3) "two"
      string(5) "three"
  </code></pre>
  
  <p>Is this possible that the classic buffer workaround with <code>ob_start()</code> doesn't work??</p>
  
  <p><strong>Note:</strong> is not important the order of output, i need to capture it for manipulation purposes</p>