doudou3213
2018-03-18 15:26
浏览 89
已采纳

PHP。 读取后,Echo不能在无限循环中工作

I'm trying write simple script which will work from console, but I ran into a problem. Echo in my case does not work. But when I delete the line: readline ('Command:'), it starts working as expected, so I tried to replace echo with print_r, but that did not help. I launch a script from the Windows console.

<?php
while (true){
    $input = readline('Command: ');
    $input = strtolower($input);
    echo $input;                    //don't work
    switch ($input){
        case 'stop':
            exit(0);
            break;
        case '1':
            echo 1;                //don't work
            break;
        default:
            echo 'Command ' . $input . ' not found.';  //don't work
    }
}
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douhun8647 2018-03-18 15:35
    已采纳

    From the user comments on the documentation - http://php.net/manual/en/function.readline.php#104181 ...

    The readline library is not available on Windows.

    if (PHP_OS == 'WINNT') {
      echo '$ ';
      $line = stream_get_line(STDIN, 1024, PHP_EOL);
    } else {
      $line = readline('$ ');
    }
    

    So try

      echo 'Command: ';
      $input = stream_get_line(STDIN, 1024, PHP_EOL);
    

    As has been pointed out, as of PHP 7.1, this function is available on Windows, but prior to that it looks as though you can do the above.

    打赏 评论

相关推荐 更多相似问题