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.

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

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line