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.

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

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题