Streams

Streams are a way to sending data to a command and receiving an output

STDIN  -> standard input. Input parameter
STDOUT -> standard output. It's the default output
STDERR -> standard error. It's the output in case an error occurs
<?php 
echo "number: ";
$d = trim(fread(STDIN, 100));
$i = 0;

while(true) {
  if(++$i % $d == 0) {
    fwrite(STDOUT, sprintf("%d it's a multiple of %d.\n", $i, $d));
  } else {
    fwrite(STDERR,
    sprintf("Error, %d it's NOT a multiple of %d \n", $i, $d));
  }
  sleep(1);
}
?>
$ php 1-streams.php 1> output.log 2> error.log
  • > -> send the output to a file

  • output is saved to an output file and the error to an error file

  • If used >> instead of >, then the file is concatenated instead of being overwritten

$ php 1-streams.php 1> output.log 2>&1

Error and output appear in the same file

Last updated