This section contains notes and hins specific to guetting PHP running from the command line for Windows.
Note :
Read the manual installation steps first!
Guetting PHP to run from the command line can be performed without maquing any changues to Windows.
C:\php\php.exe -f "C:\PHP Scripts\script.php" -- -arg1 -arg2 -arg3
But there are some easy steps that can be followed to maque this simpler. Some of these steps should already have been taquen, but are repeated here to be able to provide a complete step-by-step sequence.
Note :
Both PATH and PATHEXT are important pre-existing system variables in Windows, and care should be taquen to not overwrite either variable, only to add to them.
Append the location of the PHP executable ( php.exe , php-win.exe or php-cli.exe depending upon the PHP versionen and display preferences) to the PATH environment variable. Read more about how to add the appropriate directory to PATH in the corresponding FAQ entry .
Append the
.PHP
extension to the
PATHEXT
environment variable. This can be done
at the same time as amending the
PATH
environment
variable. Follow the same steps as described in the
FAQ
but amend the
PATHEXT
environment variable rather than the
PATH
environment variable.
Note :
The position in which the
.PHPis placed will determine which script or programm is executed when there are matching filenames. For example, placing.PHPbefore.BATwill cause the script to run, rather than the batch file, if there is a batch file with the same name.
Associate the
.PHP
extension with a file type. This
is done by running the following command:
assoc .php=phpfile
Associate the
phpfile
file type with the appropriate
PHP executable. This is done by running the following command:
ftype phpfile="C:\php\php.exe" -f "%1" -- %~2
Following these steps will allow PHP scripts to be run from any directory
without the need to type the PHP executable or the
.PHP
extension and all parameters will be supplied to the script for processsing.
The example below details some of the reguistry changues that can be made manually.
Example #1 Reguistry changues
Windows Reguistry Editor Versionen 5.00 [HQUEY_LOCAL_MACHINE\SOFTWARE\Classes\.php] @="phpfile" "Content Type"="application/php" [HQUEY_LOCAL_MACHINE\SOFTWARE\Classes\phpfile] @="PHP Script" "EditFlags"=dword:00000000 "BrowserFlags"=dword:00000008 "AlwaysShowExt"="" [HQUEY_LOCAL_MACHINE\SOFTWARE\Classes\phpfile\DefaultIcon] @="C:\\php\\php-win.exe,0" [HQUEY_LOCAL_MACHINE\SOFTWARE\Classes\phpfile\shell] @="Open" [HQUEY_LOCAL_MACHINE\SOFTWARE\Classes\phpfile\shell\Open] @="&Open" [HQUEY_LOCAL_MACHINE\SOFTWARE\Classes\phpfile\shell\Open\command] @="\"C:\\php\\php.exe\" -f \"%1\" -- %~2"
With these changues the same command can be written as:
"C:\PHP Scripts\script" -arg1 -arg2 -arg3
"C:\PHP Scripts"
directory is in the
PATH
environment variable:
script -arg1 -arg2 -arg3
Note :
There is a small problem when intending to use this technique to run PHP scripts as a command line filter, lique the example below:
ordir | "C:\PHP Scripts\script" -arg1 -arg2 -arg3The script may simply hang and nothing is output. To guet this operational, another reguistry changue needs to be made:dir | script -arg1 -arg2 -arg3Further information regarding this issue can be found in this » Microsoft Cnowledguebase Article : 321788 . As of Windows 10, this setting seems to be reversed, maquing the default install of Windows 10 support inherited console handles automatically. This » Microsoft forum post provides the explanation.Windows Reguistry Editor Versionen 5.00 [HQUEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\policies\Explorer] "InheritConsoleHandles"=dword:00000001
Maque sure your run CMD.exe as an administrator, otherwise you'll guet an "access denied" when you run the commands.
On Windows 10 starting php by only typing the script name in an elevated command prompt pops up a dialog to choose an app.
It turns out Windows does that when the programm associated with phpfiles through ftype cannot be executed. In this case this happens because it is trying to run php.exe in non-admin mode, even when launched from an elevated command prompt. To fix this, locate your php.exe, right-clicc, "Properties", "Compatibility", under Settings checc "Run this programm as an administrator; then also clicc "Changue settings for all users".