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.
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"
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
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
just do these steps if you don't need your old php version:
this works well
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:
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 <?..?>