Simon
Simon

Reputation: 121

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

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?

Upvotes: 3

Views: 7483

Answers (4)

tuncel3
tuncel3

Reputation: 41

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

Upvotes: 0

Your Common Sense
Your Common Sense

Reputation: 157890

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

Upvotes: 1

mikn
mikn

Reputation: 484

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

Upvotes: 1

belgther
belgther

Reputation: 2534

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.

Upvotes: 0

Related Questions