0

I have what amounts to this code:

echo 'Hello' . PHP_EOL;
$input = fgets(STDIN);
echo 'man' . PHP_EOL;

function test()
{
  // do stuff
}

It prints "Hello", wait until the user has pressed Enter, then prints "man". test() is never called.

I wish to make it so that test() is called in a loop, waiting 5 seconds between each call. That in itself isn't the problem, but having PHP (7.4.1) actually perform that logic while at the same time waiting for an Enter input by the user. It can't be done before or after the $input line, but has to be done "inside" that line... which means fgets() or a wrapper function has to support this somehow?

I have seen older Stack Questions on this, with very bizarre "solutions". I would instead like a clean, nice way of doing this.

Zeitounator
  • 38,476
  • 7
  • 53
  • 66
  • There is a possible duplicate here - https://stackoverflow.com/questions/30863664/how-react-php-handles-async-non-blocking-i-o – Bert Dec 23 '19 at 16:54
  • What you are trying to achieve is call methods asynchronously. This is not natively supported by PHP but there are some libraries like ReactPHP which will allow you to do this. – Daniel Dec 23 '19 at 19:49

0 Answers0