Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.