39

I am trying to learn how to access PHP scripts from the command line (CLI)

Below is an image from my attempt, please help. I am running Windows 7

Cli

5 Answers 5

94

You need to add your PHP installation directory to the %PATH% environment variable, or work from the PHP installation directory.

To add it to path (The best approach - Edited for Windows 7):

  • Right-click on a My Computer icon
  • Click Properties
  • Click Advanced system settings from the left nav
  • Click Advanced tab
  • Click Environment Variables button
  • In the System Variables section, select Path (case-insensitive) and click Edit button
  • Add a semi-colon (;) to the end of the string, then add the full file system path of your PHP installation (e.g. C:\Program Files\PHP)
  • Keep clicking OK etc until all dialog boxes have disappeared
  • Close your command prompt and open it again
  • Sorted

Alternatively, you can run cd <PHP installation path> before you try and run you command, or call your script like <FULL file system path of php.exe> <path to script>

5
  • 2
    On Windows 8: Press WIN + Q, enter "environ" and choose the first entry "Edit the system environment variables", saves you some mouse clicks. Proceed at step 5.
    – Patrick
    Nov 9, 2014 at 21:28
  • 1
    It's also worth noting that on all versions of Windows you can open the system properties dialog (first two steps) by hitting Win + pause/break
    – DaveRandom
    Nov 9, 2014 at 21:54
  • I tried this on windows 10 and it said php.exe not reconized, are there any other steps i need to take? Nov 3, 2016 at 0:23
  • @DustinPoissant are you sure you tried to use it in a new command prompt? You can type set path in a command prompt to inspect the current value of the %path% environment variable and check that the installation path of PHP is present in that session
    – DaveRandom
    Nov 3, 2016 at 8:38
  • 2
    I restarted my computer and it work. Apparently with windows 10 after you add something to the PATH you have to restart sometimes. Nov 3, 2016 at 18:10
13

For temporary use, try this: (benefit for use npm or composer)

> SET PATH=c:\wamp\bin\php\php5.3.29\;%PATH%

For test use this:

> echo %PATH%
c:\wamp\bin\php\php5.3.29\;c:\anothers\;

Now you can access to PHP command:

> php -v
PHP 5.3.29 (cli)
1
  • 1
    this trick works on my PHP 8.0 and PHP 7.4
    – STA
    Apr 24, 2022 at 18:31
6

Easy Way: Run CMD then type

C:\wamp\bin\php\php5.4.3\php.exe -f "C:\wamp\www\timenow.php"

Change the last part for a different script, and make sure you have the same version php folder as I do. (php5.4.3)

1
  • Life saver. Thank you :) Sep 2, 2019 at 12:13
4

On 2020 and with Windows 10 there's an easier way to achieve this.

1- Download composer at https://getcomposer.org/Composer-Setup.exe

2- Install it as you install any program on windows and (important) when it asks you the path to PHP, browse thru the correct path (for example: c:/wamp/bin/php.exe)

FINNISHED! Close the cmd and reopen and you can run PHP on your command line and also COMPOSER (composer is actually very needed if you use PHP)

0

you can run cd before you try and run you command, or call your script like

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.