Jump to content

Reading From Command Line


dsfsdfsdfsdf

Recommended Posts

Hi,

 

I want to be able to have an infinite loop that is exited when the user types "q" on the command line. This is pretty trivial:

 

while(1)

{

    if (trim(fgets(STDIN) == "q")

    {

        exit;

    }

}

 

 

The loop however holds and waits for user input. I searched the php.net site and in the user comments I found this function:

 

function fgets_u($pStdn)

{

$pArr = array($pStdn);

 

if (false === ($num_changed_streams = stream_select($pArr, $write = NULL, $except = NULL, 0)))

{

print("\$ 001 Socket Error : UNABLE TO WATCH STDIN.\n");

return FALSE;

}

elseif ($num_changed_streams > 0)

{

return trim(fgets($pStdn, 1024));

}

 

This function is much closer to what I would like to achieve but if you change focus on the commandline window the script halts and waits for input.

 

Any ideas?

 

Regards

 

P.S. To clarify I am looking to get my script running like a daemon that you could exit with a "q"

Link to comment
https://forums.phpfreaks.com/topic/69475-reading-from-command-line/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.