How To Run PHP From Windows Command Line in WAMPServer
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 isC:\wamp\bin\php\php5.4.3
).
When i go toRun
->CMD
-> Typephp -a
and hit enter, it saysinteractive mode enabled
. But when I writeecho 'Hi';
it shows nothing.
I even don't see anything like 'php >" when i typephp -a
and hit enter.
Answer
Solution:
The PHP CLI as its called ( php for the Command Line Interface ) is called php.exe It lives in
c:\wamp\bin\php\php5.x.y\php.exe
( where x and y are the version numbers of php that you have installed )If you want to create php scrips to run from the command line then great its easy and very useful.
Create yourself a batch file like this, lets call it
phppath.cmd
:Change
x.y.z
to a valid folder name for a version of PHP that you have installed within WAMPServerSave this into one of your folders that is already on your PATH, so you can run it from anywhere.
Now from a command window, cd into your source folder and run >phppath.
Then run
It should work like a dream.
Here is an example that configures PHP Composer and PEAR if required and they exist
Call this command file like this to use the default version of PHP
Or to get a specific version of PHP like this
Answer
Answer
Answer
Answer
Answer
Answer
Answer
Answer
Solution:
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:
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
Answer
Solution:
Try using batch file
php -S localhost:8000
.bat
extension,server.bat
server.bat
file your server is ready onhttp://localhost:8000
Dependency
if you got error php not recognize any internal or external command then goto environment variable and edit path to php.exe
"C:\wamp\bin\php\php5.4.3"
Answer
Solution:
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
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:
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:So, if that is correct, your options are:
Answer
Solution:
If you want to just run a quick code snippet you can use the -r option:
-r allows to run code without using script tags
<?..?>
Answer
Solution:
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"
Answer
Solution:
UPDATED After few research, best solution was to use that info another stackoverflow thread to avoid ctrl+z input and also from the scree output. So, instead of
php -a
you should usecall "php.exe" -f NAMED_SCRIPT.php
OLD Readline not possible under Windows, so none of existent php shells written in php will work. But there's a workaround using -a interactive mode.
2 commmon problems here. You cannot see result until executes CTRL Z command to indicate the final of code/file like EOF. When you do, result in most cases is printed result and fast closed window. Anyway, you will be returned to cmd not the -a interactive mode.
Save this content into a .bat file, and define your PHP PATH into Windows variables, or modify php.exe to "full path to exe" instead:
This is a simple Batch launching -a mode of php.exe. When it launchs php, stop script even no pause is wrote because is "into" the interactive waiting for input. When you hit CTRL Z, gets the SIGSTEP (next step) not the SIGSTOP (close, CTRL+C usually), then read the next intruction, wich is a recursive call to .bat itself. Because you're always into PHP -a mode, no exit command. You must use CTRL+C or hit the exit cross with mouse. (No alt+f4)
You can also use "Bat to Exe" converter to easy use.
Answer
Solution:
That is because you are in 'Interactive Mode' where php evaluates everything you type. To see the end result, you do 'ctrl+z' and Enter. You should see the evaluated result now :)
p.s. run the cmd as Administrator!
Answer
Solution:
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
Answer
Solution:
just do these steps if you don't need your old php version:
this works well
Answer
Solution:
In windows, put your php.exe file in windows/system32 or any other system executable folders and then go to command line and type php and hit enter following it, if it doesnt generate any error then you are ready to use PHP on command line. If you have set your php.exe somewhere else than default system folders then you need to set the path of it in the environment variables! You can get there in following path....
control panel -> System -> Edith the environment variables of your account -> Environment Vaiables -> path -> edit then set the absolute path of your php.exe there and follow the same procedure as in first paragraph, if nothing in the error department, then you are ready to use php from command line!
Answer
Solution:
A slight improvement on RiggsFolly's script above, if you set:
and add your new PHP ver path at the beginning; this allows you to set a default path in your Environment setting and then you only need this script when you want to change to a different version.
Also, if like me, you want to run this in a git bash shell, just call make a bash script to call the .bat file:
Answer
Solution:
The PHP CLI as its called ( php for the Command Line Interface ) is called php.exe It lives in
c:\wamp\bin\php\php5.x.y\php.exe
( where x and y are the version numbers of php that you have installed )If you want to create php scrips to run from the command line then great its easy and very useful.
Create yourself a batch file like this, lets call it
phppath.cmd
:Change
x.y.z
to a valid folder name for a version of PHP that you have installed within WAMPServerSave this into one of your folders that is already on your PATH, so you can run it from anywhere.
Now from a command window, cd into your source folder and run >phppath.
Then run
It should work like a dream.
Here is an example that configures PHP Composer and PEAR if required and they exist
Call this command file like this to use the default version of PHP
Or to get a specific version of PHP like this
Answer
Answer
Answer
Answer
Answer
Answer
Answer
Answer
Solution:
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:
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
Answer
Solution:
Try using batch file
php -S localhost:8000
.bat
extension,server.bat
server.bat
file your server is ready onhttp://localhost:8000
Dependency
if you got error php not recognize any internal or external command then goto environment variable and edit path to php.exe
"C:\wamp\bin\php\php5.4.3"
Answer
Solution:
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
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:
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:So, if that is correct, your options are:
Answer
Solution:
If you want to just run a quick code snippet you can use the -r option:
-r allows to run code without using script tags
<?..?>
Answer
Solution:
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"
Answer
Solution:
UPDATED After few research, best solution was to use that info another stackoverflow thread to avoid ctrl+z input and also from the scree output. So, instead of
php -a
you should usecall "php.exe" -f NAMED_SCRIPT.php
OLD Readline not possible under Windows, so none of existent php shells written in php will work. But there's a workaround using -a interactive mode.
2 commmon problems here. You cannot see result until executes CTRL Z command to indicate the final of code/file like EOF. When you do, result in most cases is printed result and fast closed window. Anyway, you will be returned to cmd not the -a interactive mode.
Save this content into a .bat file, and define your PHP PATH into Windows variables, or modify php.exe to "full path to exe" instead:
This is a simple Batch launching -a mode of php.exe. When it launchs php, stop script even no pause is wrote because is "into" the interactive waiting for input. When you hit CTRL Z, gets the SIGSTEP (next step) not the SIGSTOP (close, CTRL+C usually), then read the next intruction, wich is a recursive call to .bat itself. Because you're always into PHP -a mode, no exit command. You must use CTRL+C or hit the exit cross with mouse. (No alt+f4)
You can also use "Bat to Exe" converter to easy use.
Answer
Solution:
That is because you are in 'Interactive Mode' where php evaluates everything you type. To see the end result, you do 'ctrl+z' and Enter. You should see the evaluated result now :)
p.s. run the cmd as Administrator!
Answer
Solution:
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
Answer
Solution:
just do these steps if you don't need your old php version:
this works well
Answer
Solution:
In windows, put your php.exe file in windows/system32 or any other system executable folders and then go to command line and type php and hit enter following it, if it doesnt generate any error then you are ready to use PHP on command line. If you have set your php.exe somewhere else than default system folders then you need to set the path of it in the environment variables! You can get there in following path....
control panel -> System -> Edith the environment variables of your account -> Environment Vaiables -> path -> edit then set the absolute path of your php.exe there and follow the same procedure as in first paragraph, if nothing in the error department, then you are ready to use php from command line!
Answer
Solution:
A slight improvement on RiggsFolly's script above, if you set:
and add your new PHP ver path at the beginning; this allows you to set a default path in your Environment setting and then you only need this script when you want to change to a different version.
Also, if like me, you want to run this in a git bash shell, just call make a bash script to call the .bat file: