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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?