【问题标题】:Stop executing PHP for or while loop in Command line with key press?用按键停止在命令行中执行 PHP for 或 while 循环?
【发布时间】:2011-11-29 10:10:02
【问题描述】:

php 是否可以在“按键”时退出循环?

例如连续for循环在按键时退出并继续执行其余代码而不是sigterm,只需停止循环并继续?

【问题讨论】:

    标签: php command-line-interface


    【解决方案1】:

    您想要做的是从标准输入读取: http://linux.about.com/library/cmd/blcmdl3_stdin.htm

    这里有一个关于如何在 PHP 中执行此操作的教程: http://codegolf.com/boards/conversation/view/129

    希望有帮助!

    编辑:找到一个回答得更好的问题:PHP CLI: How to read a single character of input from the TTY (without waiting for the enter key)?

    【讨论】:

    • 谢谢。我已经尝试过使用 STDIN,但是,你抓取输入会停止我需要的脚本:while {exec_something;听按键...;如果发生按键则离开}
    • 这里的这个问题其实好像有你要找的答案:stackoverflow.com/questions/3684367/…
    【解决方案2】:

    Ctrl-C 击键将停止循环,我相信 ;-)

    【讨论】:

    • 第二。只需 ctrl-c 即可中止正在运行的 php 进程。
    • Ctrl-C 是 SIGINT。但不能在 Windows 上捕获 :(
    【解决方案3】:

    对于 Windows,您可以使用 API 函数来检索键盘状态。调用 Win32 API 函数的方法在http://de.php.net/manual/en/ref.w32api.php 中有描述。您需要调用的 API 函数是 GetKeyboardState,结果存储在数组中。

    【讨论】:

    • 谢谢,但我正在寻找在命令行 php 脚本中在 LINUX 上执行此操作的方法
    【解决方案4】:

    按键盘上的 ctrl+pausebreak 键停止在 PHP 命令 shell 中运行的循环。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-23
      • 2017-06-20
      • 1970-01-01
      • 2018-03-23
      • 2021-12-25
      • 2012-06-26
      • 1970-01-01
      相关资源
      最近更新 更多