How To Run PHP From Windows Command Line in WAMPServer

前端 未结 11 693
予麋鹿
予麋鹿 2020-11-22 04:58

I\'m new to php and wanted to run php from command line. I have installed WAMP and set the \"System Variables\" to my php folder ( which is C:\\wamp\\bin\\php\\php5.4.

相关标签:
11条回答
  • 2020-11-22 05:17

    You can run php pages using php.exe create some php file with php code and in the cmd write "[PATH to php.ext]\php.exe [path_to_file]\file.php"

    0 讨论(0)
  • 2020-11-22 05:18

    I remember one time when I stumbled upon this issue a few years ago, it's because windows don't have readline, therefore no interactive shell, to use php interactive mode without readline support, you can do this instead:

    C:\>php -a 
    Interactive mode enabled 
    
    <?php 
    echo "Hello, world!"; 
    ?> 
    ^Z 
    Hello, world!
    

    After entering interactive mode, type using opening (<?php) and closing (?>) php tag, and end with control Z (^Z) which denotes the end of file.

    I also recall that I found the solution from php's site user comment: http://www.php.net/manual/en/features.commandline.interactive.php#105729

    0 讨论(0)
  • 2020-11-22 05:19

    The following solution is specifically for wamp environments:

    This foxed me for a little while, tried all the other suggestions, $PATH etc even searched the windows registry looking for clues:

    The GUI (wampmanager) indicates I have version 7 selected and yes if I phpinfo() in a page in the browser it will tell me its version 7.x.x yet php -v in the command prompt reports a 5.x.x

    If you right click on the wampmanager head to icon->tools->delete unused versions and remove the old version, let it restart the services then the command prompt will return a 7.x.x

    This solution means you no longer have the old version if you want to switch between php versions but there is a configuration file in C:\wamp64\wampmanager.conf which appears to specify the version to use with CLI (the parameter is called phpCliVersion). I changed it, restarted the server ... thought I had solved it but no effect perhaps I was a little impatient so I have a feeling there may be some mileage in that.

    Hope that helps someone

    0 讨论(0)
  • 2020-11-22 05:21

    just do these steps if you don't need your old php version:

    • open wamp and right click on wamp manager than go : tools/Change PHP CLI Version than change php version to latest
    • another time right click on wamp manager than go : tools/Delete unuserd versions and delete the oldest version which your system insist on it to be your pc php version :D
    • go to control panel/user account/change my environment variables and in PATH variable click edit and add your latest php version path which is in your wamp server bin folder
    • close all command lines or IDEs and restart them and check for php -v

    this works well

    0 讨论(0)
  • 2020-11-22 05:24

    The problem you are describing sounds like your version of PHP might be missing the readline PHP module, causing the interactive shell to not work. I base this on this PHP bug submission.

    Try running

    php -m
    

    And see if "readline" appears in the output.

    There might be good reasons for omitting readline from the distribution. PHP is typically executed by a web server; so it is not really need for most use cases. I am sure you can execute PHP code in a file from the command prompt, using:

    php file.php
    

    There is also the phpsh project which provides a (better) interactive shell for PHP. However, some people have had trouble running it under Windows (I did not try this myself).

    Edit: According to the documentation here, readline is not supported under Windows:

    Note: This extension is not available on Windows platforms.

    So, if that is correct, your options are:

    • Avoid the interactive shell, and just execute PHP code in files from the command line - this should work well
    • Try getting phpsh to work under Windows
    0 讨论(0)
  • 2020-11-22 05:24

    If you want to just run a quick code snippet you can use the -r option:

    php -r "echo 'hi';"
    

    -r allows to run code without using script tags <?..?>

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