No output from PHP interactive on Windows

前端 未结 8 2046
轻奢々
轻奢々 2020-12-16 13:28

I\'m running php interactively from xampp (5.4.7) on my Win 7 machine and cannot get any output from the window. I searched around various solutions and nothing so far has

相关标签:
8条回答
  • 2020-12-16 13:33

    As you can read from the other answers, Windows PHP does not support echoing commands(easily). On Windows 10 there is a way around this however, you can enable the Windows 10 Ubuntu sub-shell system.

    To install windows-subsystem for linux

    Do one of the following:

    • Control Panel -> Apps -> Administrate optional features. Then make sure Windows-subsystem for linux is checked.

    • You can also install it by running Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux in an elevated Powershell Window.

    Now reboot(maybe bookmark this page first)

    Once you've rebooted install this app and open it. When it's done loading and you've created your user, type sudo apt update && apt upgrade && apt install PHP.

    Now when you need php interactive just open command prompt and type bash then php -a

    0 讨论(0)
  • 2020-12-16 13:35

    As of PHP 7.1.0 both the interactive shell and readline are supported on Windows.

    0 讨论(0)
  • 2020-12-16 13:40

    When using the Interactive Mode, you literally need to input the whole script.

    <?php
    echo "HELLO WORLD";
    

    Then to "execute" the script, you need to do CTRL+Z on an empty line and press ENTER

    0 讨论(0)
  • 2020-12-16 13:43

    I make cmd script for emulate interactive php shell. It reads each command into an environment variable and then starts a new instance of PHP to run each command.

    @echo off
    :loop
        set /p php="php> " %=%
        php -r "%php%"
        echo.
    goto loop
    
    0 讨论(0)
  • 2020-12-16 13:44

    Interactive mode is not the same as Interactive shell. The later accepts commands like in cmd or a shell found in linux. The first one reads in the whole script and then returns with the output. If you would hit CTRL-Z at the end of the snippet it should return the output.

    Check here: http://php.net/manual/en/features.commandline.interactive.php for more information (especially the first comment).

    0 讨论(0)
  • 2020-12-16 13:47

    If you start "php -a" and see "Interactive mode enabled" instead of "Interactive shell", then your copy of php was likely compiled without readline support, and there is no interactive shell.

    As far as I know, Windows copies of PHP are all compiled without readline support. This makes the "-a" option worthless in Windows (at least to me).

    "php -a" is supposed to work like this (output from a linux copy of php in this case):

    $ php -a
    Interactive shell
    
    php > echo 5+8;
    13
    php > exit
    $
    

    "php -a" in a Windows copy of PHP is not an interactive shell, and so effectively works the same as leaving off the "-a" option. Use F6 or CTRL+Z to type the EOF character to finish the script (appears as ^Z):

    C:\>php -a
    Interactive mode enabled
    
    <?php
    echo 5+8;
    ?>
    ^Z
    13
    C:\>
    

    Leave off the "-a" and you get essentially the same results (minus the text "Interactive mode enabled":

    C:\>php
    <?php
    echo 5+8;
    ?>
    ^Z
    13
    C:\>
    

    If you use "php -a" and get the text "Interactive mode enabled" and don't get the output for cli.prompt (usually "php >"), then you should check your php version (with "php -v") to make sure it is at least 5.1, and your php modules (with "php -m") to make sure the "readline" module is listed.

    0 讨论(0)
提交回复
热议问题