dsfsdfsdfsdf Posted September 15, 2007 Share Posted September 15, 2007 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" Quote Link to comment https://forums.phpfreaks.com/topic/69475-reading-from-command-line/ Share on other sites More sharing options...
rarebit Posted September 15, 2007 Share Posted September 15, 2007 Try: if(!fgets(STDIN)) Quote Link to comment https://forums.phpfreaks.com/topic/69475-reading-from-command-line/#findComment-349075 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.