Command Line PHP on Microsoft Windows

This section contains notes and hints specific to getting PHP running from the command line for Windows.

Hinweis:

You should 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 -arg3

But 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.

    Hinweis:

    Both PATH and PATHEXT are important pre-existing system variables in Windows, and care should be taken 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 your PHP version and display preferences) to the PATH environment variable. Read more about how to add your PHP 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.

    Hinweis:

    The position in which you place the .PHP will determine which script or program is executed when there are matching filenames. For example, placing .PHP before .BAT will cause your 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 processing.

The example below details some of the registry changes that can be made manually.

Beispiel #1 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 -arg3
or, if your "C:\PHP Scripts" path is in the PATH environment variable:
script -arg1 -arg2 -arg3

Hinweis:

There is a small problem if you intend to use this technique and use your PHP scripts as a command line filter, like the example below:

dir | "C:\PHP Scripts\script" -arg1 -arg2 -arg3
or
dir | script -arg1 -arg2 -arg3
You may find that the script simply hangs and nothing is output. To get this operational, you need to make another registry change.
Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\policies\Explorer]
"InheritConsoleHandles"=dword:00000001
Further information regarding this issue can be found in this » 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. This »  Microsoft forum post provides the explanation.

Hier Kannst Du einen Kommentar verfassen


Bitte gib mindestens 10 Zeichen ein.
Wird geladen... Bitte warte.
* Pflichtangabe
Es sind noch keine Kommentare vorhanden.

Total Commander 11.00 (final) ist verfügbar!

Total Commander, ein beliebter Datei-Manager, hat kürzlich seine neueste Version, 11.00, veröffentlicht. Mit einer Vielzahl von neuen Funktionen und Korrekturen bietet diese Version den Benutzern ein verbessertes und optimiertes Erlebnis. ...

admin

Autor : admin
Kategorie: Software-Updates

Welche Technologien sollte ein PHP Programmierer beherrschen?

In der heutigen digitalen Welt ist die Rolle eines PHP Programmierers von großer Bedeutung. ...

admin

Autor : admin
Kategorie: Dies und Das

PHPUnit in der Version 10 erschienen

PHPUnit in der Version 10 erschienen

Das beliebte Testframeworks PHPUnit ist in der Version 10 erschienen und steht zum Download bereit ...

TheMax

Autor : TheMax
Kategorie: Software & Web-Development

Tutorial veröffentlichen

Tutorial veröffentlichen

Teile Dein Wissen mit anderen Entwicklern weltweit

Du bist Profi in deinem Bereich und möchtest dein Wissen teilen, dann melde dich jetzt an und teile es mit unserer PHP-Community

mehr erfahren

Tutorial veröffentlichen

Fragen zu PHP-Syntax

Dankeschön, deine Erklärungen lassen mich Beides schon einmal besser nachvollziehen. Bei 2) hatte ich mittlerweile etwas zu Namensräumen gefund ...

Geschrieben von BrotherJ am 12.09.2023 08:05:17
Forum: PHP Developer Forum
Datei Prüfung - Alles was zwischen /* und */ steht, einfärben.

In deinem Beispiel endet der Kommentar in Zeile 11. Die Zeilen 12 bis 16 sind nicht mehr eingeschlossen, denn Kommentarzeichen kann man so nicht v ...

Geschrieben von reddighamburg am 12.09.2023 00:57:14
Forum: PHP Developer Forum
Fragen zu PHP-Syntax

zu 1) Hier geht es um Typensicherheit. Das : Response sagt nichts anderes, als dass der zurückgegebene Wert in jedem Fall vom Typ Response ist. A ...

Geschrieben von reddighamburg am 12.09.2023 00:31:02
Forum: PHP Developer Forum
Google findet die Seite einfach nicht...

Hey, Es tut mir leid zu hören, dass du Schwierigkeiten mit der Indexierung deines Blogs hast. In solchen Fällen ist es wichtig, verschiedene As ...

Geschrieben von Nikolla am 08.09.2023 12:31:56
Forum: SEO - Suchmaschinen Tricks und Tipps