PHP loop listen for user input -
i have php script run on console.
while(1) { dostuff(); sleep(2); }
i need accept input console. don't want loop stop each time , wait me input text.
what want while loop continue normal, if type in console, php script able read text , update variable.
can done ?
you can non-blocking i/o. you'll need stream_set_blocking
method , stream_select
:
stream_set_blocking(stdin, false); while (1) { dostuff(); $readstreams = [stdin]; $timeout = 2; // stream_select block $timeout seconds or until stdin // contains data read. $numberofstreamswithdata = stream_select( $readstreams, $writestreams = [], $except = [], $timeout ); if ($numberofstreamswithdata > 0) { $userinput = fgets(stdin); // process $userinput see fit } else { // no user input; repeat loop normal } }
Comments
Post a Comment