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:

RSS feed Subscribe to my RSS feed

About the author

Jani is a 15 year veteran of the software industry. He's currently available for consulting

  1. 13 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

  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)