Running PHP on the command line on Windows systems
This section contains notes and hints specific to getting PHP running from the command line for Windows.
This section contains notes and hints specific to getting PHP running from the command line for Windows.
Read the manual installation steps first!
Getting PHP to run from the command line can be performed without making any changes to Windows.
C:\php\php.exe -f "C:\PHP Scripts\script.php" -- -arg1 -arg2 -arg3But there are some easy steps that can be followed to make this simpler. Some of these steps should already have been taken, but are repeated here to be able to provide a complete step-by-step sequence.
- Append the location of the PHP executable (
php.exe,php-win.exeorphp-cli.exedepending upon the PHP version and display preferences) to thePATHenvironment variable. Read more about how to add the appropriate directory toPATHin the corresponding FAQ entry. Append the
.PHPextension to thePATHEXTenvironment variable. This can be done at the same time as amending thePATHenvironment variable. Follow the same steps as described in the FAQ but amend thePATHEXTenvironment variable rather than thePATHenvironment variable.NoteThe position in which the
.PHPis placed will determine which script or program 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
.PHPextension with a file type. This is done by running the following command:outputassoc .php=phpfileAssociate the
phpfilefile type with the appropriate PHP executable. This is done by running the following command:outputftype 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 processing.
The example below details some of the registry changes that can be made manually.
Registry changes
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\.php]
@="phpfile"
"Content Type"="application/php"
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\phpfile]
@="PHP Script"
"EditFlags"=dword:00000000
"BrowserFlags"=dword:00000008
"AlwaysShowExt"=""
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\phpfile\DefaultIcon]
@="C:\\php\\php-win.exe,0"
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\phpfile\shell]
@="Open"
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\phpfile\shell\Open]
@="&Open"
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\phpfile\shell\Open\command]
@="\"C:\\php\\php.exe\" -f \"%1\" -- %~2"With these changes the same command can be written as:
"C:\PHP Scripts\script" -arg1 -arg2 -arg3or, if the "C:\PHP Scripts" directory is in the PATH environment variable:
script -arg1 -arg2 -arg3There is a small problem when intending to use this technique to run PHP scripts as a command line filter, like the example below:
dir | "C:\PHP Scripts\script" -arg1 -arg2 -arg3or
dir | script -arg1 -arg2 -arg3The script may simply hang and nothing is output. To get this operational, another registry change needs to be made:
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\policies\Explorer]
"InheritConsoleHandles"=dword:00000001Further information regarding this issue can be found in Microsoft Knowledgebase Article 321788. As of Windows 10, this setting seems to be reversed, making the default install of Windows 10 support inherited console handles automatically.