Environment Variables PATH

Advertisement

The PATH is the system variable in the Windows Operating System.

It contains the executables files path to run them from the command line or Terminal window.

NOTE: Making changes to the system PATH variable is typically not necessary for computers running Windows or Mac OS X. If you are sure about the executable files then you can do it.

In this article we are going to see:

Understand Environment Variables Understand Environment Variables

Most of us face some of the below issues while developing.

Example 1: PHP – Set Environment Variable

We know that PHP is a server-side scripting language. But, We can execute the PHP script through the command link too.

So, Without time waste, Let’s execute the PHP script through the command line with the below example.

Note: I assume that you have a local host server XAMPP, MAMP, WAMP, VVV, Local By Flywheel, or something else.

  • Step 1: Create a file test.php in c:\xampp\htdocs\examples
  • Step 2: Copy and paste the below code into the file test.php
<?php
echo 'Hello World';
  • Step 3: Open Command Prompt or terminal
  • Step 4: Type the command cd c:\xampp\htdocs\examples
  • Step 5: Type the command php test.php

You may see the below error:

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

Why? Why do we get an error?

Because by default Windows operating system does not have any inbuild php command.

Now, Before making the PHP command in working condition lets us understands a few things:

First, locate the executable PHP file called php.exe.

I have installed XAMPP, which is installed on the location C:\xampp\

and the location of my php.exe file is in C:\xampp\php\php.exe

Okay.

Now, In Step 5 we used the command:

php test.php

Instead of the above command try:

C:\xampp\php\php.exe test.php

you can see something like the below:

c:\xampp\htdocs\examples>C:\xampp\php\php.exe test.php
Hello World 

Yup! we see that our text Hello World is now printed in the terminal window.

Now, Understand that the file php.exe execute the code from the PHP file test.php.

So, If we don’t have set the environment path then we can use the C:\xampp\php\php.exe test.php instead of php test.php.

But, Always use the path C:\xampp\php\php.exe is it more time-consuming?

So, Let’s add our executable PHP file path C:\xampp\php\ into the windows environment variable to make php command as a recognized command.

Top ↑

Set Environment Variable Set Environment Variable

  • Step 1: Search for “system environment variables” and click on it.
  • Step 2: Click “Environment Variables”. In the section System Variables,
  • Step 3: Select the “Path” from “System Variables” and click edit.
  • Step 4: Click “New” and add the path “C:\xampp\php\” and click “Ok”.
  • Step 5: Close or Reopen the Command prompt window.

Check the below image for reference:

Windows Environment Path Flow
Windows Environment Path Flow

Now, Execute our PHP file test.php with the below steps:

  • Step 1: Type the command cd c:\xampp\htdocs\examples
  • Step 2: Type the command php test.php

You can see something like this below:

c:\xampp\htdocs\examples>php test.php
 Hello World

Now, We don’t need to use C:\xampp\php\php.exe.

Top ↑

Command Error Messages Command Error Messages

Below are the most common error messages while trying to use commands.

NOTE: All below error will fix if we set their executable files path into the window’s environment path.

‘PHP’ is not recognized as an internal or external command ‘PHP’ is not recognized as an internal or external command

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

Read more fixing the PHP is not a recognized internal or external command

Top ↑

‘MySql’ is not recognized as an internal or external command ‘MySql’ is not recognized as an internal or external command

'mysql' is not recognized as an internal or external command,<br>operable program or batch file.

Read more fixing MySQL is not a recognized internal or external command

Top ↑

‘Node’ is not recognized as an internal or external command ‘Node’ is not recognized as an internal or external command

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

Read more fixing Node is not a recognized internal or external command

Top ↑

‘NPM’ is not recognized as an internal or external command ‘NPM’ is not recognized as an internal or external command

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

Read more fixing NPM is not a recognized internal or external command

Top ↑

‘WP’ is not recognized as an internal or external command ‘WP’ is not recognized as an internal or external command

'wp' is not recognized as an internal or external command,<br>operable program or batch file.

Read more about installing and using the WP CLI on Windows Operating System.

Top ↑

Frequently Asked Questions Frequently Asked Questions

Top ↑

What is an environment variable? What is an environment variable?

An environment variable is a dynamic value that can affect how programs and processes run on a computer.

Top ↑

What is the PATH environment variable? What is the PATH environment variable?

The PATH environment variable is a variable that contains a list of directories separated by semicolons.

It is used by the operating system to locate executable files.

Top ↑

How can I view my PATH environment variable? How can I view my PATH environment variable?

In Windows, you can view the PATH environment variable by opening the Command Prompt and typing “echo %PATH%“.

In Unix-based systems, you can type “echo $PATH” in the Terminal.

Top ↑

How can I add a directory to my PATH environment variable? How can I add a directory to my PATH environment variable?

In Windows, you can add a directory to your PATH environment variable by going to System Properties > Advanced > Environment Variables, selecting the “PATH” variable, and clicking “Edit”.

Then, add the directory you want to include at the end of the list, separated by a semicolon.

In Unix-based systems, you can add a directory to your PATH environment variable by editing the ~/.bashrc or ~/.bash_profile file and adding the directory to the PATH variable.

Top ↑

What happens if I have multiple directories with the same executable file in my PATH environment variable? What happens if I have multiple directories with the same executable file in my PATH environment variable?

When you run the executable file, the operating system will execute the first instance of the file that it finds in the directories listed in your PATH environment variable.

To ensure that the correct version of the file is being used, you can reorder the directories in your PATH variable to prioritize the directory containing the correct version of the file.

Top ↑

What is the default value of the PATH environment variable? What is the default value of the PATH environment variable?

The default value of the PATH environment variable varies depending on the operating system and the version.

In Windows, the default value typically includes directories such as C:\Windows\System32 and C:\Windows\System32\WindowsPowerShell\v1.0.

In Unix-based systems, the default value typically includes directories such as /usr/local/bin and /usr/bin.

Leave a Reply