Related
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.
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 :
PATH=%PATH%;c:\wamp\bin\php\phpx.y.z
php -v
Change x.y.z to a valid folder name for a version of PHP that you have installed within WAMPServer
Save 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
php your_script.php
It should work like a dream.
Here is an example that configures PHP Composer and PEAR if required and they exist
#echo off
REM **************************************************************
REM * PLACE This file in a folder that is already on your PATH
REM * Or just put it in your C:\Windows folder as that is on the
REM * Search path by default
REM * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
REM * EDIT THE NEXT 3 Parameters to fit your installed WAMPServer
REM **************************************************************
set baseWamp=D:\wamp
set defaultPHPver=7.4.3
set composerInstalled=%baseWamp%\composer
set phpFolder=\bin\php\php
if %1.==. (
set phpver=%baseWamp%%phpFolder%%defaultPHPver%
) else (
set phpver=%baseWamp%%phpFolder%%1
)
PATH=%PATH%;%phpver%
php -v
echo ---------------------------------------------------------------
REM IF PEAR IS INSTALLED IN THIS VERSION OF PHP
IF exist %phpver%\pear (
set PHP_PEAR_SYSCONF_DIR=%baseWamp%%phpFolder%%phpver%
set PHP_PEAR_INSTALL_DIR=%baseWamp%%phpFolder%%phpver%\pear
set PHP_PEAR_DOC_DIR=%baseWamp%%phpFolder%%phpver%\docs
set PHP_PEAR_BIN_DIR=%baseWamp%%phpFolder%%phpver%
set PHP_PEAR_DATA_DIR=%baseWamp%%phpFolder%%phpver%\data
set PHP_PEAR_PHP_BIN=%baseWamp%%phpFolder%%phpver%\php.exe
set PHP_PEAR_TEST_DIR=%baseWamp%%phpFolder%%phpver%\tests
echo PEAR INCLUDED IN THIS CONFIG
echo ---------------------------------------------------------------
) else (
echo PEAR DOES NOT EXIST IN THIS VERSION OF php
echo ---------------------------------------------------------------
)
REM IF A GLOBAL COMPOSER EXISTS ADD THAT TOO
REM **************************************************************
REM * IF A GLOBAL COMPOSER EXISTS ADD THAT TOO
REM *
REM * This assumes that composer is installed in /wamp/composer
REM *
REM **************************************************************
IF EXIST %composerInstalled% (
ECHO COMPOSER INCLUDED IN THIS CONFIG
echo ---------------------------------------------------------------
set COMPOSER_HOME=%baseWamp%\composer
set COMPOSER_CACHE_DIR=%baseWamp%\composer
PATH=%PATH%;%baseWamp%\composer
rem echo TO UPDATE COMPOSER do > composer self-update
echo ---------------------------------------------------------------
) else (
echo ---------------------------------------------------------------
echo COMPOSER IS NOT INSTALLED
echo ---------------------------------------------------------------
)
set baseWamp=
set defaultPHPver=
set composerInstalled=
set phpFolder=
set phpver=
set phpFolder=
Call this command file like this to use the default version of PHP
> phppath
Or to get a specific version of PHP like this
> phppath 5.6.30
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
Try using batch file
Open notepad
type php -S localhost:8000
save file as .bat extension, server.bat
now click on server.bat file your server is ready on http://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"
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
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 <?..?>
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 use call "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:
::
:: PHP Shell launch wrapper
::
#ECHO off
call "php.exe" -a
echo.
echo.
call "PHP Shell.bat"
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.
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!
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:
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
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!
A slight improvement on RiggsFolly's script above, if you set:
PATH=%phpver%;%PATH%
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:
#!/bin/bash
eval phppath.bat $1
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.
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 :
PATH=%PATH%;c:\wamp\bin\php\phpx.y.z
php -v
Change x.y.z to a valid folder name for a version of PHP that you have installed within WAMPServer
Save 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
php your_script.php
It should work like a dream.
Here is an example that configures PHP Composer and PEAR if required and they exist
#echo off
REM **************************************************************
REM * PLACE This file in a folder that is already on your PATH
REM * Or just put it in your C:\Windows folder as that is on the
REM * Search path by default
REM * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
REM * EDIT THE NEXT 3 Parameters to fit your installed WAMPServer
REM **************************************************************
set baseWamp=D:\wamp
set defaultPHPver=7.4.3
set composerInstalled=%baseWamp%\composer
set phpFolder=\bin\php\php
if %1.==. (
set phpver=%baseWamp%%phpFolder%%defaultPHPver%
) else (
set phpver=%baseWamp%%phpFolder%%1
)
PATH=%PATH%;%phpver%
php -v
echo ---------------------------------------------------------------
REM IF PEAR IS INSTALLED IN THIS VERSION OF PHP
IF exist %phpver%\pear (
set PHP_PEAR_SYSCONF_DIR=%baseWamp%%phpFolder%%phpver%
set PHP_PEAR_INSTALL_DIR=%baseWamp%%phpFolder%%phpver%\pear
set PHP_PEAR_DOC_DIR=%baseWamp%%phpFolder%%phpver%\docs
set PHP_PEAR_BIN_DIR=%baseWamp%%phpFolder%%phpver%
set PHP_PEAR_DATA_DIR=%baseWamp%%phpFolder%%phpver%\data
set PHP_PEAR_PHP_BIN=%baseWamp%%phpFolder%%phpver%\php.exe
set PHP_PEAR_TEST_DIR=%baseWamp%%phpFolder%%phpver%\tests
echo PEAR INCLUDED IN THIS CONFIG
echo ---------------------------------------------------------------
) else (
echo PEAR DOES NOT EXIST IN THIS VERSION OF php
echo ---------------------------------------------------------------
)
REM IF A GLOBAL COMPOSER EXISTS ADD THAT TOO
REM **************************************************************
REM * IF A GLOBAL COMPOSER EXISTS ADD THAT TOO
REM *
REM * This assumes that composer is installed in /wamp/composer
REM *
REM **************************************************************
IF EXIST %composerInstalled% (
ECHO COMPOSER INCLUDED IN THIS CONFIG
echo ---------------------------------------------------------------
set COMPOSER_HOME=%baseWamp%\composer
set COMPOSER_CACHE_DIR=%baseWamp%\composer
PATH=%PATH%;%baseWamp%\composer
rem echo TO UPDATE COMPOSER do > composer self-update
echo ---------------------------------------------------------------
) else (
echo ---------------------------------------------------------------
echo COMPOSER IS NOT INSTALLED
echo ---------------------------------------------------------------
)
set baseWamp=
set defaultPHPver=
set composerInstalled=
set phpFolder=
set phpver=
set phpFolder=
Call this command file like this to use the default version of PHP
> phppath
Or to get a specific version of PHP like this
> phppath 5.6.30
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
Try using batch file
Open notepad
type php -S localhost:8000
save file as .bat extension, server.bat
now click on server.bat file your server is ready on http://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"
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
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 <?..?>
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 use call "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:
::
:: PHP Shell launch wrapper
::
#ECHO off
call "php.exe" -a
echo.
echo.
call "PHP Shell.bat"
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.
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!
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:
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
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!
A slight improvement on RiggsFolly's script above, if you set:
PATH=%phpver%;%PATH%
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:
#!/bin/bash
eval phppath.bat $1
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.
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 :
PATH=%PATH%;c:\wamp\bin\php\phpx.y.z
php -v
Change x.y.z to a valid folder name for a version of PHP that you have installed within WAMPServer
Save 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
php your_script.php
It should work like a dream.
Here is an example that configures PHP Composer and PEAR if required and they exist
#echo off
REM **************************************************************
REM * PLACE This file in a folder that is already on your PATH
REM * Or just put it in your C:\Windows folder as that is on the
REM * Search path by default
REM * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
REM * EDIT THE NEXT 3 Parameters to fit your installed WAMPServer
REM **************************************************************
set baseWamp=D:\wamp
set defaultPHPver=7.4.3
set composerInstalled=%baseWamp%\composer
set phpFolder=\bin\php\php
if %1.==. (
set phpver=%baseWamp%%phpFolder%%defaultPHPver%
) else (
set phpver=%baseWamp%%phpFolder%%1
)
PATH=%PATH%;%phpver%
php -v
echo ---------------------------------------------------------------
REM IF PEAR IS INSTALLED IN THIS VERSION OF PHP
IF exist %phpver%\pear (
set PHP_PEAR_SYSCONF_DIR=%baseWamp%%phpFolder%%phpver%
set PHP_PEAR_INSTALL_DIR=%baseWamp%%phpFolder%%phpver%\pear
set PHP_PEAR_DOC_DIR=%baseWamp%%phpFolder%%phpver%\docs
set PHP_PEAR_BIN_DIR=%baseWamp%%phpFolder%%phpver%
set PHP_PEAR_DATA_DIR=%baseWamp%%phpFolder%%phpver%\data
set PHP_PEAR_PHP_BIN=%baseWamp%%phpFolder%%phpver%\php.exe
set PHP_PEAR_TEST_DIR=%baseWamp%%phpFolder%%phpver%\tests
echo PEAR INCLUDED IN THIS CONFIG
echo ---------------------------------------------------------------
) else (
echo PEAR DOES NOT EXIST IN THIS VERSION OF php
echo ---------------------------------------------------------------
)
REM IF A GLOBAL COMPOSER EXISTS ADD THAT TOO
REM **************************************************************
REM * IF A GLOBAL COMPOSER EXISTS ADD THAT TOO
REM *
REM * This assumes that composer is installed in /wamp/composer
REM *
REM **************************************************************
IF EXIST %composerInstalled% (
ECHO COMPOSER INCLUDED IN THIS CONFIG
echo ---------------------------------------------------------------
set COMPOSER_HOME=%baseWamp%\composer
set COMPOSER_CACHE_DIR=%baseWamp%\composer
PATH=%PATH%;%baseWamp%\composer
rem echo TO UPDATE COMPOSER do > composer self-update
echo ---------------------------------------------------------------
) else (
echo ---------------------------------------------------------------
echo COMPOSER IS NOT INSTALLED
echo ---------------------------------------------------------------
)
set baseWamp=
set defaultPHPver=
set composerInstalled=
set phpFolder=
set phpver=
set phpFolder=
Call this command file like this to use the default version of PHP
> phppath
Or to get a specific version of PHP like this
> phppath 5.6.30
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
Try using batch file
Open notepad
type php -S localhost:8000
save file as .bat extension, server.bat
now click on server.bat file your server is ready on http://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"
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
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 <?..?>
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 use call "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:
::
:: PHP Shell launch wrapper
::
#ECHO off
call "php.exe" -a
echo.
echo.
call "PHP Shell.bat"
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.
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!
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:
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
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!
A slight improvement on RiggsFolly's script above, if you set:
PATH=%phpver%;%PATH%
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:
#!/bin/bash
eval phppath.bat $1
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.
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 :
PATH=%PATH%;c:\wamp\bin\php\phpx.y.z
php -v
Change x.y.z to a valid folder name for a version of PHP that you have installed within WAMPServer
Save 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
php your_script.php
It should work like a dream.
Here is an example that configures PHP Composer and PEAR if required and they exist
#echo off
REM **************************************************************
REM * PLACE This file in a folder that is already on your PATH
REM * Or just put it in your C:\Windows folder as that is on the
REM * Search path by default
REM * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
REM * EDIT THE NEXT 3 Parameters to fit your installed WAMPServer
REM **************************************************************
set baseWamp=D:\wamp
set defaultPHPver=7.4.3
set composerInstalled=%baseWamp%\composer
set phpFolder=\bin\php\php
if %1.==. (
set phpver=%baseWamp%%phpFolder%%defaultPHPver%
) else (
set phpver=%baseWamp%%phpFolder%%1
)
PATH=%PATH%;%phpver%
php -v
echo ---------------------------------------------------------------
REM IF PEAR IS INSTALLED IN THIS VERSION OF PHP
IF exist %phpver%\pear (
set PHP_PEAR_SYSCONF_DIR=%baseWamp%%phpFolder%%phpver%
set PHP_PEAR_INSTALL_DIR=%baseWamp%%phpFolder%%phpver%\pear
set PHP_PEAR_DOC_DIR=%baseWamp%%phpFolder%%phpver%\docs
set PHP_PEAR_BIN_DIR=%baseWamp%%phpFolder%%phpver%
set PHP_PEAR_DATA_DIR=%baseWamp%%phpFolder%%phpver%\data
set PHP_PEAR_PHP_BIN=%baseWamp%%phpFolder%%phpver%\php.exe
set PHP_PEAR_TEST_DIR=%baseWamp%%phpFolder%%phpver%\tests
echo PEAR INCLUDED IN THIS CONFIG
echo ---------------------------------------------------------------
) else (
echo PEAR DOES NOT EXIST IN THIS VERSION OF php
echo ---------------------------------------------------------------
)
REM IF A GLOBAL COMPOSER EXISTS ADD THAT TOO
REM **************************************************************
REM * IF A GLOBAL COMPOSER EXISTS ADD THAT TOO
REM *
REM * This assumes that composer is installed in /wamp/composer
REM *
REM **************************************************************
IF EXIST %composerInstalled% (
ECHO COMPOSER INCLUDED IN THIS CONFIG
echo ---------------------------------------------------------------
set COMPOSER_HOME=%baseWamp%\composer
set COMPOSER_CACHE_DIR=%baseWamp%\composer
PATH=%PATH%;%baseWamp%\composer
rem echo TO UPDATE COMPOSER do > composer self-update
echo ---------------------------------------------------------------
) else (
echo ---------------------------------------------------------------
echo COMPOSER IS NOT INSTALLED
echo ---------------------------------------------------------------
)
set baseWamp=
set defaultPHPver=
set composerInstalled=
set phpFolder=
set phpver=
set phpFolder=
Call this command file like this to use the default version of PHP
> phppath
Or to get a specific version of PHP like this
> phppath 5.6.30
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
Try using batch file
Open notepad
type php -S localhost:8000
save file as .bat extension, server.bat
now click on server.bat file your server is ready on http://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"
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
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 <?..?>
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 use call "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:
::
:: PHP Shell launch wrapper
::
#ECHO off
call "php.exe" -a
echo.
echo.
call "PHP Shell.bat"
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.
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!
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:
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
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!
A slight improvement on RiggsFolly's script above, if you set:
PATH=%phpver%;%PATH%
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:
#!/bin/bash
eval phppath.bat $1
After screwing up entirely my PHP configuration on MAC trying to get the SOAP module working (-bash: /usr/bin/php: No such file or directory
....) I now have to use MAMP but each time I have to type the path
Applications/MAMP/bin/php5.3/bin/php to do command line.
How to just type php instead the entire path on MAC ? I double checked and i do not have a file named .profile nor bash_profile
Thanks
PS: Here's what output echo $PATH :
echo $PATH
/Applications/MAMP/Library/bin/:/Applications/MAMP/bin/php5/bin/:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/git/bin:/usr/X11/bin
In your home folder /Users/David for exmaple, you can create a .bash_profile. In here you can export variables and then add them to your path.
Open up the file to edit it in your favourite editor, I use vim.
Then you can add in your path
export MAMP_PHP=/Applications/MAMP/bin/php/php5.3.6/bin
export PATH="$MAMP_PHP:$PATH"
You want your bit ahead of the $PATH as that already includes /usr/bin which is where the system PHP lives. So the system will always find your MAMP version first.
Save this file and then reboot your Terminal and you'll see that you should get your MAMP version.
To test I use php -v as OSX Lion uses 5.3.10 and my MAMP is using 5.3.6
You can also test using which php which will output the path to your current php executable.
The fact that the previously accepted answer refers to php 5.3.6, while the current version of MAMP ships with 7.2.1 as the default (as of early 2018), points out that this is not a very sustainable solution. You can make your path update automatically by adding an extra line to your .bash_profile or .zshrc to get the latest version of PHP from /Applications/MAMP/bin/php/ and export that to your path. Here’s how I do it:
# Use MAMP version of PHP
PHP_VERSION=`command ls /Applications/MAMP/bin/php/ | sort -n | tail -1`
export PATH=/Applications/MAMP/bin/php/${PHP_VERSION}/bin:$PATH
(Use source ~/.bash_profile after making your changes to make sure they take effect.)
As others have mentioned, you will likely also want to modify your shell to use MAMP’s mysql executable, which is located in /Applications/MAMP/Library/bin. However, I do not recommend exporting that folder, because there are a bunch of other executables there, like libtool, that you probably don’t want to be giving priority to over your system installed versions. This issue prevented me from installing a node package recently (libxmljs), as documented here.
My solution was to define and export mysql and mysqladmin as functions:
# Export MAMP MySQL executables as functions
# Makes them usable from within shell scripts (unlike an alias)
mysql() {
/Applications/MAMP/Library/bin/mysql "$#"
}
mysqladmin() {
/Applications/MAMP/Library/bin/mysqladmin "$#"
}
export -f mysql
export -f mysqladmin
I used functions instead of aliases, because aliases don’t get passed to child processes, or at least not in the context of a shell script. The only downside I’ve found is that running which mysql and which mysqladmin will no longer return anything, which is a bummer. If you want to check which mysql is being used and make sure everything is copacetic, use mysql --version instead.
Note: #julianromera points out that zsh doesn’t support exporting functions, so in that case, you’re best off using an alias, like alias mysql='/Applications/MAMP/Library/bin/mysql'. Just be aware that your aliases might not be available from subshells (like when executing a shell script).
Everytime you save MAMP config (PHP section), it saves the current version of PHP on ~/.profile file and creates the alias for php, pear and pecl, to point to the current configured version. (Note: you need to check "Make this version available on the command line" option in MAMP)
However, you need to refresh your terminal (open another session) to get this file refreshed. You can also type source ~/.profile to refesh the aliases manually.
If you want to extract this curerent version in a PHP_VERSION variable - as commented above - for further use, you can do:
export PHP_VERSION=`grep "alias php" ~/.profile | cut -d"/" -f6 | cut -c4-`
And then you'll have $PHP_VERSION available with the current version of MAMP.
Finally, if you want to run your php using the current configured version on mamp, you just need to add to your ~/.bash_profile the following:
export PHP_VERSION=`grep "alias php" ~/.profile | cut -d"/" -f6 | cut -c4-`
export PHPRC="/Library/Application Support/appsolute/MAMP PRO/conf/" #point to your php.ini folder to use the same php settings
export PATH=/Applications/MAMP/bin/php/php$PHP_VERSION/bin:$PATH
Now, even script that relies on /usr/bin/env php will read the correct version from Mamp config.
I found that on Mavericks 10.8 there wasn't a .bash_profile and my paths were located in /etc/paths
In order to have the new path (whether this is a mamp or brew install of php) take effect it needs to be above the default /usr/bin/php in this paths file. eg.
/Applications/MAMP/bin/php/php5.3.6/bin
/usr/bin
AFter the change, open a new terminal window and run 'which php' that should now point to your updated path
you might still run into mysql binary not being found in that manner
open terminal, type
touch ~/.bash_profile; open ~/.bash_profile
edit as follows below, save, quite and restart terminal or alternately
source ~/.bash_profile
to execute new PATH without restarting terminal
and in the fashion of the DavidYell's post above, also add the following. You can stack various variables by exporting them followed by a single PATH export which I demonstrated below
export MAMP_PHP=/Applications/MAMP/bin/php/php5.6.2/bin
export MAMP_BINS=/Applications/MAMP/Library/bin
export USERBINS=~/bins
export PATH="$USERBINS:$MAMP_PHP:$MAMP_BINS:$PATH"
cheers
If you have to type
/Applications/MAMP/bin/php5.3/bin/php
in your command line then add
/Applications/MAMP/bin/php5.3/bin
to your PATH to be able to call php from anywhere.
For XAMPP users you can use this:
# Use XAMPP version of PHP
export PATH=/Applications/XAMPP/xamppfiles/bin:$PATH
source ~/.bash_profile
And you can check it with:
php -v
This one worked for me:
sudo mv /usr/bin/php /usr/bin/~php
sudo ln -s /Application/XAMPP/xamppfiles/bin/php /usr/bin/php
Sometimes, it's easier to do this:
sudo ln -s /Applications/MAMP/bin/php/php5.6.10/bin/php /usr/bin/php;
Mamps version of PHP at the time of posting was php5.6.10, so make sure you change that to the version you're using.
You'll be up in a jiffy.
Probably too late to comment but here's what I did when I ran into issues with setting php PATH for my XAMPP installation on Mac OSX
Open up the file .bash_profile (found under current user folder) using the available text editor.
Add the path as below:
export PATH=/path/to/your/php/installation/bin:leave/rest/of/the/stuff/untouched/:$PATH
Save your .bash_profile and re-start your Mac.
Explanation: Terminal / Mac tries to run a search on the PATHS it knows about, in a hope of finding the program, when user initiates a program from the "Terminal", hence the trick here is to make the terminal find the php, the user intends to, by pointing it to the user's version of PHP at some bin folder, installed by the user.
Worked for me :)
P.S I'm still a lost sheep around my new Computer ;)
The latest version of MAMP (Version 5+) offers an easy way to make the MAMP PHP version available to the command line. Just select "PHP" in the the side bar menu and check "Make this version available on the command line". Easy peasy! See attached screenshot:)
This is not an ideal solution as you have to manage two ini files however, I managed to work around this on windows by copying the php ini file in mamp from the conf folder to your active php version in the bin folder.
[MAMP INSTALL]\conf\[ACTIVE PHP VERSION]\php.ini
copy to
[MAMP INSTALL]\bin\php\[ACTIVE PHP VERSION]
To compliment the current accepted answer, if you assume that MAMP uses the most recent version of php5 as the default, you can add grep 'php5' in the middle:
PHP_VERSION= `ls /Applications/MAMP/bin/php/ | sort -n | grep 'php5' | tail -1`
and you are guaranteed to get the most recent php5 regardless of MAMP version.