Windows 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 localhost 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 command cd c:\xampp\htdocs\examples
  • Step 5: Type 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 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 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 below steps:

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

You can see something like 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.

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,
operable program or batch file.

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.

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.

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,
operable program or batch file.

%d bloggers like this: