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

JasonDavis
  • 48,204
  • 100
  • 318
  • 537

5 Answers5

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>

DaveRandom
  • 87,921
  • 11
  • 154
  • 174
  • 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 09 '14 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 09 '14 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? – Dustin Poissant Nov 03 '16 at 00: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 03 '16 at 08: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. – Dustin Poissant Nov 03 '16 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)
Nabi K.A.Z.
  • 9,887
  • 6
  • 59
  • 81
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)

Mikeys4u
  • 1,494
  • 18
  • 26
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)

gtamborero
  • 2,898
  • 27
  • 28
0

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