问题
Is it possible in php to exit a loop on a "key press"?
For example continual for loop to exit on key press and to continue executing rest of code so not sigterm, just stop loop and continue?
回答1:
What you want to do is read from stdin: http://linux.about.com/library/cmd/blcmdl3_stdin.htm
Here's a tutorial for how to do it in PHP: http://codegolf.com/boards/conversation/view/129
Hope that helps!
Edit: Found a question which answered this much better: PHP CLI: How to read a single character of input from the TTY (without waiting for the enter key)?
回答2:
Ctrl-C
keystroke will stop the loop, I believe ;-)
回答3:
for Windows, you can use API functions to retrieve keyboard state. The way to call Win32 API functions is described in http://de.php.net/manual/en/ref.w32api.php. The API function you need to call is GetKeyboardState where the result is stored in an array.
来源:https://stackoverflow.com/questions/8308997/stop-executing-php-for-or-while-loop-in-command-line-with-key-press