3

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?

Richard Povinelli
  • 1,419
  • 1
  • 14
  • 28
Simon
  • 121
  • 7
  • 19

4 Answers4

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)?

Community
  • 1
  • 1
mikn
  • 484
  • 3
  • 7
  • Thanks for this. I already tried with STDIN but, you grabbing input stops the script I need something like: while {exec_something; listen for key presss...; if key press occured then leave} – Simon Nov 29 '11 at 11:30
  • This question here actually seems to have the answer you're looking for: http://stackoverflow.com/questions/3684367/php-cli-how-to-read-a-single-character-of-input-from-the-tty-without-waiting-f – mikn Nov 29 '11 at 11:36
1

Ctrl-C keystroke will stop the loop, I believe ;-)

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
0

Press ctrl+pausebreak keys on your keyboard to stop a loop which is running in PHP command shell.

tuncel3
  • 21
  • 5
0

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.

belgther
  • 2,544
  • 17
  • 15