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.3
).
When i go to Run
-> CMD
-> Type php -a
and hit enter, it says interactive mode enabled
. But when I write echo 'Hi';
it shows nothing.
I even don't see anything like 'php >" when i type php -a
and hit enter.
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
<?..?>
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"
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.
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