Loading...
X

How to add PHP path to %PATH% environment variable on Windows

What is PATH and what is it for

It is quite possible that you have never encountered echo %PATH% and environment variable expressions before, so I will briefly explain what it is.

The PATH variable contains a list of folders in which Windows looks for executable files.

In the GUI, when shortcuts are used to launch programs, the PATH value is not very large. But if you run the program on the command line, then PATH can come in handy. Again, if you specify the full path to the file, for example, C:\Users\Alex\Documents\php.exe, then PATH is not used. But if, for example, you run the program only by the file name or just by the name (without the file extension), then whether the program starts will depend on the contents of the PATH variable.

For example, I try to run a file on the command line (without specifying the full path)

php.exe

In this case, the operating system will look at all PATH entries (several directories may be specified there). Windows will then try to find the php.exe file in each of these directories. If the file is found, it will be launched. If the file is not found, a corresponding message will be displayed.

In fact, only those who work a lot with the command line need to add something to the PATH variable. For example, you are a programmer and you place your programs in the C:\MyApps folder and you often run your command line utilities. In this case, you can add C:\MyApps to PATH and after that, to run programs from this folder, you no longer need to enter the full name each time (for example, C:\MyApps\parser.exe), but it will be enough in the command line enter only the filename:

parser.exe

Do I need to add PHP to environment variable on Windows

When installing and configuring PHP on Windows, it is not necessary to add the path to PHP to the PATH, but it is recommended to do so.

First, you can run PHP using the shorthand:

php my_script.php

instead

C:\Server\bin\PHP\php.exe my_script.php

Secondly, a number of extensions (which are included in the php.ini file) do not work correctly if you do not write the path to PHP in the PATH; this also applies to such a rather popular extension as cURL. In theory, this is some kind of bug of these extensions or PHP, but we ourselves will have to fix the situation, since this problem has existed for many years.

How to add PHP to system environment variables

So let's get started.

In the search box start typing “Edit the system environment variables

and open the corresponding settings window.

There click on “Environment Variables”, it will open:

In the “System variables” window, find and click on Path, then click “Edit”.

Then click the “New” button and enter “C:\Server\bin\PHP\” there:

Raise the entry to the very top:

Close all windows and save your changes.

Restart the server.


One observation on “How to add PHP path to %PATH% environment variable on Windows
  1. Sammy

    If you are macOS user, ServBay.dev is a good tool, especially for the beginners. It handles all PHP, MariaDB, PostgreSQL versions, plus Redis and Memcached. Run multiple PHP instances simultaneously and switch easily. This tool has made my PHP dev simpler. Worth a shot!

     
    Reply

Leave Your Observation

Your email address will not be published. Required fields are marked *