Reading line by line from STDIN without blocking

Basically, I'm looking to read strings from STDIN, but I don't want to block while waiting for new data. Almost like using a thread 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);
+5
source share
1 answer

Remove it, use stream_set_blocking Docs to disable the lock. Sets $lineto falsewhen input is not available.

+7
source

All Articles