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" 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)) Link to comment https://forums.phpfreaks.com/topic/69475-reading-from-command-line/#findComment-349075 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.