Stop executing PHP for or while loop in Command line with key press?

*爱你&永不变心* 提交于 2020-01-02 09:37:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!