164

I got the following error when I run a command with php

C:\xampp\htdocs>php
'php' is not recognized as an internal or external command,
operable program or batch file.

I don't get any error when I run the command with php in following path:

C:\xampp\php>php  //do not got error here

Why I get this error?

'php' is not recognized as an internal or external command, operable program or batch file.

Paolo Forgia
  • 6,572
  • 8
  • 46
  • 58
Sadikhasan
  • 18,365
  • 21
  • 80
  • 122

17 Answers17

265

Add C:\xampp\php to your PATH environment variable.(My Computer->properties -> Advanced system setting-> Environment Variables->path (click on edit))

Then close your command prompt and restart again.

Note: It's very important to close your command prompt and restart again otherwise changes will not be reflected.

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
161

You need to Go to My Computer->properties -> Advanced system setting

Now click on Environment Variables..

enter image description here

Add ;C:\xampp\php in path variable value

enter image description here

Now restart command prompt DONE!

Note: Make sure you run CMD via run as administrator

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
Prince Patel
  • 2,990
  • 1
  • 21
  • 28
  • is that variable is "path" or Path? – Muhammad Ali Sep 24 '21 at 07:38
  • @MuhammadAli There's only one Path variable in there. In the Environment Variables window, in the "System Variables" section, double click on the Path variable. This will take you to the "Edit environment variable" window. Click new and add the "C:\xampp\php\". – MJ DLS Jul 06 '23 at 09:07
20

You just need to a add the path of your PHP file. In case you are using wamp or have not installed it on the C drive.

The picture shows how to find the path

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Progga Ilma
  • 578
  • 6
  • 8
14

Set "C:\xampp\php" in your PATH Environment Variable. Then restart CMD prompt.

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Vikas
  • 267
  • 2
  • 10
8

You need to add C:\xampp\php to your PATH Environment Variable, Only after then you would be able to execute php command line from outside php_home.

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Raja
  • 851
  • 9
  • 22
6

Is your path correctly configured?

In Windows, you can do that as described here:

http://www.computerhope.com/issues/ch000549.htm

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
5

For Laragon:

First, download your PHP from [https://windows.php.net/download/] then unzip your file -> copy folder -> paste this direction:

C:\laragon\bin\php`Your folder`

search from windows

Edit the system environment variables and then open it. Edit the system environment variables

Click Environment variables... Environment variables

Click path and Edit

environment varible add

Click New and add the php.exe path like: C:\laragon\bin\php`Your folder`

and then press OK for all steps.

restart laragon and vscode

and our problem is already solved.

Antu Paul
  • 161
  • 2
  • 5
4

enter image description here enter image description here

Here what I DO on MY PC I install all software that i usually used in G: partian not C: if my operating system is fall (win 10) , Do not need to reinstall them again and lost time , Then How windows work it update PATH automatic if you install any new programe or pice of softwore ,

SO

I must update PATH like these HERE! all my software i usually used

%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;G:\HashiCorp\Vagrant\bin;G:\xampp\php;G:\xampp\mysql\bin;G:\Program Files (x86)\heroku\bin;G:\Program Files (x86)\Git\bin;G:\Program Files (x86)\composer;G:\Program Files (x86)\nodejs;G:\Program Files (x86)\Sublime Text 3;G:\Program Files (x86)\Microsoft VS Code\bin;G:\Program Files (x86)\cygwin64\bin

enter image description here

Abubakr Elghazawy
  • 977
  • 14
  • 21
3

I also got the following error when I run a command with PHP, I did the solution like that:

  1. From the desktop, right-click the Computer icon.
  2. Choose Properties from the context menu.
  3. Click the Advanced system settings link.
  4. Click Environment Variables. In the section System Variables, find the PATH environment variable and select it. Click Edit. If the PATH environment variable does not exist, click New.
  5. In the Edit System Variable window, Add C:\xampp\php to your PATH Environment Variable.

Very important note: restart command prompt

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Udhav Sarvaiya
  • 9,380
  • 13
  • 53
  • 64
  • This error occurs when you try to use by command. With the given solution, your environment variable path will be corrected, But if you do not close the command prompt after changing the path, then old path will work only. if you have not restarted your command prompt then the changes will not be reflected. After changing the environment variable path, close the command prompt (If the command prompt was open before making changes) and reuse the command prompt for further use. – Udhav Sarvaiya Oct 29 '18 at 05:44
3

Extra info:

If you are using PhpStorm as IDE, after updating the path variable you need to restart PhpStorm so that it takes effect.

Restarting terminal window was not enough for me. (PhpStorm 2020.3.2)

Adem Tepe
  • 564
  • 5
  • 10
3

In latest Xampp 8.1.2 PHP path is changed to this below: Set it in your PATH variable.

C:\xampp\php\windowsXamppPhp

Don't forget to restart the PowerShell and run as Administrator.

Abid Ali
  • 679
  • 7
  • 10
2

I have to face the same problem which I solved by adding the path of XAMP/WAMP to PATH environment variable.

Follow these steps:

  1. Go to System Properties
  2. Go to Advanced
  3. Go to Environmental Variables
  4. Edit the Path environment variable
  5. Add the installation path of xammp/wamp like in my case the path is E:\xammp\php
  6. Click on Ok
  7. Restart command prompt (cmd)
  8. Check the version of PHP by running php --version.

kiner_shah
  • 3,939
  • 7
  • 23
  • 37
Anwar Zahid
  • 227
  • 3
  • 10
2

I faced this issue even with a properly set up windows environment variable. I just needed to close and restart the terminal to solve it.

S6rra
  • 61
  • 2
  • It's needed only when you updated PATH with already opened terminal. In that case of course you need to restart the terminal. – Max Aug 08 '21 at 10:31
2

The previous answers about adding the path to the Env Var are correct, but my case was different.

I needed to downgrade my PHP version to 5 in order to update an old application.

I was using the following combination: MAMP, PhpStorm, the Environment Variable Path was set properly to the MAMP PHP version that I was using

  • The error was showing in PhpStorm's Terminal
  • The error was due to the php.ini file using # in the comment
  • After I replaced "#" by ";" I restarted PhpStorm and recognized the PHP version and the command "php -v" worked again
lalvar
  • 71
  • 3
2

In my case the problem was in the path, I was giving the path to the php.exe and that's wrong I have to write the path of the root folder...

Sarout
  • 821
  • 4
  • 25
1

Laragon users

you will find your PHP folder in the bin folder of laragon. goto your desired PHP version folder and copy the location. for me it was:

C:\laragon\bin\php\php-7.4.19-Win32-vc15-x64

Add that location on your system PATH:

Add that location on your system PATH

After that, you need to restart your cmd or terminal.

enter image description here

Emtiaz Zahid
  • 2,645
  • 2
  • 19
  • 35
0

I restarted CMD program it solves my problem

PHPFan
  • 756
  • 3
  • 12
  • 46