Setting up command-line PHP on Windows

July 28, 2008 – 9:14 am Tags:

On *nix systems you usually have the php executable available in shell, so you can run command-line scripts like unit tests and such, but on Windows this isn’t usually the case. Let’s fix that!

There’s two ways to do this: Temporary and permanent

Temporary

The temporary method will affect only a single command prompt.

Open command prompt (start, run, cmd) and type in the following:

set PATH=%PATH%;C:\path\to\php

Where C:\path\to\php is the folder where your php.exe file is located. After this, you should be able to run PHP in any folder like this:

C:\>php -v
PHP 5.2.5 (cli) (built: Nov  8 2007 23:18:51)
Copyright (c) 1997-2007 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies
    with Zend Extension Manager v1.2.0, Copyright (c) 2003-2007, by Zend Technologies
    with Xdebug v2.0.2, Copyright (c) 2002-2007, by Derick Rethans

Your output may vary depending on your PHP version and extensions.

Permanent

The permanent method will affect all command prompts and will persist upon reboots. It can also be set up to affect all users on the system and not only your account.

  1. Open control panel
  2. Go to System
  3. Choose the Properties tab
  4. Click Environment variables

From this screen you can edit the environment variables of the system. If you only wish to modify them on your user account, click New under your own variables box and type in the following:

Variable name: Path
Variable value: %PATH%;C:\path\to\php

If you want the change to affect all users on the system, choose the Path variable from the system variables box and click Edit.

Append the variable value with:

;C:\path\to\php

Note the ; character. It’s used to separate directories in the var, so be sure it’s present.

Again, C:\path\to\php is where your php.exe is located.






Now you should have the php executable available in command line. Have fun!

Share this:
  1. 17 Responses to “Setting up command-line PHP on Windows”

  2. You might also want to check which php.ini is being used by your webserver and by your client call. They might differ (on linux they almost always do !).

    Can give some strange results if extensions aren’t enabled on cli while they are on the web version.

    By Harro on Jul 28, 2008

  3. Great. Some times we for get the nitty gritty

    By sharma chelluri on May 13, 2009

  4. Clear, just what I was needing!

    Kelvin, PR USA

    By Kelvin on Jul 14, 2009

  5. This is really very helpful information!

    By Harry on Jan 28, 2010

  6. It’s work for me :D

    both way are works!

    i’m running on win7 and xampp

    thanks :)

    By dht on Dec 18, 2011

  7. Not Working…It says php is not recognized as internal or external command.

    By drum on Jan 10, 2013

  8. It’s working!!!! :-)
    TY :-)

    By drum on Jan 10, 2013

  9. CLI is working perfectly fine now! Thanks!

    By Etamina on Jan 31, 2013

  10. thanks… very helpful

    By alok on Feb 15, 2013

  11. It is not working in windows command line:(

    By ─░pek on Mar 20, 2013

  12. Its amazing!

    After trawling the internet, I have finally found an article that gives absolute beginners like me a step by step guide as to how to do this.

    If you go onto forums, and other articles, they start from points that tend to make the whole thing pretty vague.

    Thanks codeutopia!

    By Peter on Mar 26, 2013

  13. Help! pls anyone. I’m new to programming. Installed wamp and tried to run a created php file in command prompt. gone through the procedure of setting the path in environmental variable to c:\wamp\bin\php\php5.5.12. set the location to the path of my created file, tried running it using index.php yet it just goes to the next line without any output. used the php -v syntax and it doesn’t do anything just go to the next line. If anyone can help out i’ll highly appreciate it.

    By Stroud on Jan 19, 2015

  14. Hey Stroud, make sure that the path you have is to the folder where php.exe is.

    Eg. if php.exe is in c:\wamp\bin\php.exe, the path should be c:\wamp\bin

    By Jani Hartikainen on Jan 20, 2015

  15. I have installed Pydio which can utilize PHP CLI for background tasks such as file indexing, however Pydio complains that it can’t find it, typing “php” into a regular command prompt works perfectly, PHP on the webserver (IIS) works perfectly, but the web apps that need CLI (in this case just Pydio) can’t seem to find it. I have the COM extension enabled, PHPinfo recognizes it, but for some reason Pydio doesn’t. I’m at a total loss, multiple pages have suggested various solutions that I have tried to no avail.

    The only thing that I can possibly see is that IIS does not have permissions to run cmd.exe because it’s owned by TrustedInstaller. Pydio suggests that, but since it’s owned by TrustedInstaller the admin account isn’t allowed to tamper with permissions. If there was a way I could copy cmd.exe to the inetpub directory and force PHP to use that cmd instead of the system one that would likely work because it has ownership and permissions over it. However I am lost as to what to do next. Any advice would be greatly appreciated.

    By PJSam on Jan 25, 2015

  16. I’m afraid I’m not very familiar with what you describe PJSam. Perhaps post your question to ServerFault?

    By Jani Hartikainen on Jan 25, 2015

  1. 2 Trackback(s)

  2. Jan 22, 2009: Setting up Windows as a PHP development server | CodeUtopia
  3. Jul 4, 2009: Unit testing part 2: Writing and running tests | CodeUtopia

Post a Comment

You can use some HTML (a, em, strong, etc.). If you want to post code, use <pre lang="PHP">code here</pre> (you can replace PHP with the language you are posting)