0

Please note that I am not wishing to use any bloated library such as "ReactPHP". I just wish to know how to do this in actual PHP code.

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, which I hope is now possible.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • 1
    You will have to use threads or similar asynchronous methods. – deceze Dec 26 '19 at 12:32
  • Duplicate of https://stackoverflow.com/questions/59458643/how-do-i-call-a-function-regularly-while-php-is-waiting-for-input-from-the-use – Dharman Dec 28 '19 at 21:17

0 Answers0