5

Basically, I'm looking to read lines from STDIN, but I don't want to block while waiting for new data. Almost like using a stream with a timeout.

$stdin = fopen('php://stdin', 'r');

do {
  $line = fgets($stdin);

  // No input right now
  if (empty($line)) {
    // Do something before waiting for more input
  }
} while (1);
St. John Johnson
  • 6,590
  • 7
  • 35
  • 56

1 Answers1

7

Figured it out, use stream_set_blockingDocs to disable blocking. Sets $line to false when no input is available.

hakre
  • 193,403
  • 52
  • 435
  • 836
St. John Johnson
  • 6,590
  • 7
  • 35
  • 56